Switch language
wpseek on Twitter


A WordPress-centric search engine for devs and theme authors




wp_list_filter [ ]

wp_list_filter ( $list, $args = array(), $operator = 'AND' )
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'.
Returns:
  • (array)
Defined at:
Codex



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

Source

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 ( array_key_exists( $m_key, $to_match ) && $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;
}

Top Google Search Results

More ...

User discussions [ wordpress.org ]

User Note(s)