A WordPress-centric search engine for devs and theme authors



wp_list_filter ›

Since3.1.0
Deprecatedn/a
wp_list_filter ( $list, $args = array(), $operator = 'AND' )
Parameters: (3)
  • (array) $list An array of objects to filter.
    Required: Yes
  • (array) $args Optional. An array of key => value arguments to match against each object. Default empty array.
    Required: No
    Default: array()
  • (string) $operator Optional. 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. Default 'AND'.
    Required: No
    Default: 'AND'
Returns:
  • (array) Array of found values.
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;
}