Example #1
0
/**
 * Filters a list of objects, based on a set of key => value arguments.
 *
 * @since 3.1.0
 * @since 4.7.0 Uses WP_List_Util class.
 *
 * @param array  $list     An array of objects to filter.
 * @param array  $args     Optional. An array of key => value arguments to match
 *                         against each object. Default empty array.
 * @param 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'.
 * @return array Array of found values.
 */
function wp_list_filter($list, $args = array(), $operator = 'AND')
{
    if (!is_array($list)) {
        return array();
    }
    $util = new WP_List_Util($list);
    return $util->filter($args, $operator);
}
 public function test_wp_list_util_get_output()
 {
     $expected = array((object) array('foo' => 'bar', 'bar' => 'baz'));
     $util = new WP_List_Util(array((object) array('foo' => 'bar', 'bar' => 'baz'), (object) array('bar' => 'baz')));
     $actual = $util->filter(array('foo' => 'bar'));
     $this->assertEqualSets($expected, $actual);
     $this->assertEqualSets($expected, $util->get_output());
 }