wp_list_filter

Definition:
function wp_list_filter( $list, $args = array() {}

Filters a list of objects, based on a set of key => value arguments.

Parameters

  • array $list: An array of objects to filter
  • array $args: An array of key => value arguments to match against each object
  • string $operator: The logical operation to perform: ‘AND’ means all elements from the array must match; ‘OR’ means only one element needs to match; ‘NOT’ means no elements may match. The default is ‘AND’.

Source code

function wp_list_filter( $list, $args = array(), $operator = 'AND' ) {

	if ( ! is_array( $list ) )

		return array();



	if ( empty( $args ) )

		return $list;



	$operator = strtoupper( $operator );

	$count = count( $args );

	$filtered = array();



	foreach ( $list as $key => $obj ) {

		$to_match = (array) $obj;



		$matched = 0;

		foreach ( $args as $m_key => $m_value ) {

			if ( $m_value == $to_match[ $m_key ] )

				$matched++;

		}



		if ( ( 'AND' == $operator && $matched == $count )

		  || ( 'OR' == $operator && $matched > 0 )

		  || ( 'NOT' == $operator && 0 == $matched ) ) {

			$filtered[$key] = $obj;

		}

	}



	return $filtered;

}

10844

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: