Use PHP's array_filter function with a callback.

$new = array_filter($arr, function ($var) {
    return ($var['name'] == 'CarEnquiry');
});

Edit: If it needs to be interchangeable, you can modify the code slightly:

$filterBy = 'CarEnquiry'; // or Finance etc.

$new = array_filter($arr, function (filterBy) {
    return ($var['name'] == $filterBy);
});
Answer from dchesterton on Stack Overflow
🌐
PHP
php.net › manual › en › function.array-filter.php
PHP: array_filter - Manual
Note that a filtered array no longer encodes to json arrays, as the indices are no longer continuous: $a = ['a', 'b', 'c']; var_dump(json_encode($a)); // ["a","b","c"] $a = array_filter($a, function ($x) { return $x == 'b'; }); var_dump(json_encode($a)); // {"1": "b"} you can use array_values get a continuous array var_dump(json_encode(array_values($a))); // ["b"]
🌐
CodexWorld
codexworld.com › home › how to guides › how to filter multidimensional array by key value in php
How to Filter Multidimensional Array by Key Value in PHP - CodexWorld
January 4, 2023 - PHP array_filter() function filters elements of an array by a callback function and it can be used for many purposes. The array_filter() function provides a short and simple way to filter multidimensional array by key and value.
🌐
Medium
himanshugautamdev.medium.com › php-filtering-data-from-a-multidimensional-array-in-php-b0ccda0180c3
PHP-Filtering Data from a Multidimensional Array in PHP | by Himanshu Gautam | Medium
January 8, 2024 - $filterData = array_filter($dataList, function($item) { return in_array(13, $item['skill_test']); }); ... <pre>Array ( [0] => Array ( [id] => 59 [name] => Himanshu [skill_test] => Array ( [0] => 12 [1] => 13 [2] => 14 [3] => 15 ) ) ) ... Experienced ...
🌐
WP Scholar
wpscholar.com › home › filtering multi-dimensional arrays in php
Filtering Multi-Dimensional Arrays in PHP - WP Scholar
August 14, 2015 - Filtering empty values from an array in PHP is best done using array_filter().
🌐
Koding Made Simple
kodingmadesimple.com › 2018 › 01 › filter-multidimensional-array-by-key-value-php.html
How to Filter Multidimensional Array by Key Value in PHP
Multi-dimensional arrays are ... specific key values to filter it. But PHP's array_filter() function provides a short and simple way to filter multidimensional array by key and value....
🌐
W3Schools
w3schools.com › php › func_array_filter.asp
PHP array_filter() Function
Arrays Indexed Arrays Associative ... Arrays Multidimensional Arrays Array Functions PHP Superglobals · Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions · PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete · PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters ...
🌐
W3Resource
w3resource.com › php-exercises › php-array-exercise-52.php
PHP Array Exercise: Filter a multi-dimensional array and get those items that will match with the specified value - w3resource
<?php // Function to filter a multi-dimensional array based on a specific value in a specified index function array_filter_by_value($my_array, $index, $value) { // Check if the input is a non-empty array if(is_array($my_array) && count($my_array) ...
🌐
Reddit
reddit.com › r/phphelp › how to filter a multidimensional array
r/PHPhelp on Reddit: How to filter a multidimensional array
June 22, 2022 -

Helllo ! I would like to use filter to iterate through an Array of arrays ! and to sort items which have gender = "male" ! I already used foreach loop and it works super easy by pointing the item of items item['gender']..But How can I filter a multidimensional array!

$items = [ ['name' => "Toto","age" => 47,"gender" => "Male" ], ['name' => "titi","age" => 12,"gender" => "Male" ], ['name' => "tata","age" => 13,"gender" => "Female" ]];I used this snippet$new = array_filter($items, function ($var) {return ($var['sex'] == 'Male');});echo $new;This snippest occurs the following error: Uncaught Error: Value of type null is not callable ........... Stack trace: #0 {main} thrown in

Find elsewhere
🌐
MP4Moviez
pakainfo.com › home › programming › php filter multidimensional array examples
PHP Filter Multidimensional Array Examples - Pakainfo
As I will cover this Post with live Working example to develop php get array key by value multidimensional, so the php remove empty array elements for this example is following below. ... $myArr = array( array('name' => 'Jobar shah', 'email' => '[email protected]'), array('name' => 'Mayur Lies', 'email' => '[email protected]'), array('name' => 'Hiteshy Dhameliya', 'email' => '[email protected]'), ); $strlike = 'jo'; $result = array_filter($myArr, function ($item) use ($strlike) { if (stripos($item['name'], $strlike) !== false) { return true; } return false; });
🌐
GeeksforGeeks
geeksforgeeks.org › php › how-to-search-by-keyvalue-in-a-multidimensional-array-in-php
How to search by key=>value in a multidimensional array in PHP ? - GeeksforGeeks
July 15, 2024 - The array_filter() function in PHP can be used to search for a key-value pair in a multidimensional array by providing a custom callback function.
Top answer
1 of 5
5

I decided to rewrite my answer to accommodate both filtering and sorting. I took a heavily object oriented approach to solving this problem, which I will detail below.

You can see all of this code in action at this ideone.com live demonstration.


The first thing I did was define two interfaces.

interface Filter {
    public function filter($item);
}

interface Comparator {
    public function compare($a, $b);
}

As their names suggest, Filter is used for filtering, and Comparator is used for comparing.

Next, I defined three concrete classes that implements these interfaces, and accomplish what I wanted.

First is KeyComparator. This class simply compares the key of one element to the key of another element.

class KeyComparator implements Comparator {
    protected $direction;
    protected $transform;
    protected $key;

    public function __construct($key, $direction = SortDirection::Ascending, $transform = null) {
        $this->key = $key;
        $this->direction = $direction;
        $this->transform = $transform;
    }

    public function compare($a, $b) {
        $a = $a[$this->key];
        $b = $b[$this->key];

        if ($this->transform) {
            $a = $this->transform($a);
            $b = $this->transform($b);
        }

        return $a === $b ? 0 : (($a > $b ? 1 : -1) * $this->direction);
    }
}

You can specify a sort direction, as well as a transformation to be done to each element before they are compared. I defined a helped class to encapsulate my SortDirection values.

class SortDirection {
    const Ascending = 1;
    const Descending = -1;
}

Next, I defined MultipleKeyComparator which takes multiple KeyComparator instances, and uses them to compare two arrays against each other. The order in which they are added to the MultipleKeyComparator is the order of precedence.

class MultipleKeyComparator implements Comparator {
    protected $keys;

    public function __construct($keys) {
        $this->keys = $keys;
    }

    public function compare($a, $b) {
        $result = 0;

        foreach ($this->keys as $comparator) {
            if ($comparator instanceof KeyComparator) {
                $result = $comparator->compare($a, $b);

                if ($result !== 0) return $result;
            }
        }

        return $result;
    }
}

Finally, I created MultipleKeyValueFilter which is meant to filter an array based on an array of key/value pairs:

class MultipleKeyValueFilter implements Filter {
    protected $kvPairs;

    public function __construct($kvPairs) {
        $this->kvPairs = $kvPairs;
    }

    public function filter($item) {
        $result = true;

        foreach ($this->kvPairs as $key => $value) {
            if ($item[$key] !== $value)
                $result &= false;
        }

        return $result;
    }
}

Now, given the input array (Notice I rearranged them a bit to make the sorting obvious):

$array = array (
    '1' => array ('type' => 'blah2', 'category' => 'cat2', 'exp_range' => 'this_week' ),
    '2' => array ('type' => 'blah1', 'category' => 'cat1', 'exp_range' => 'this_week' ),
    '3' => array ('type' => 'blah1', 'category' => 'cat2', 'exp_range' => 'next_week' ),
    '4' => array ('type' => 'blah1', 'category' => 'cat1', 'exp_range' => 'next_week' )
);

Sorting can be achieved by doing the following:

$comparator = new MultipleKeyComparator(array(
    new KeyComparator('type'),
    new KeyComparator('exp_range')
));

usort($array, array($comparator, 'compare'));

echo "Sorted by multiple fields\n";
print_r($array);

Filtering can be achieved by doing the following:

$filter = new MultipleKeyValueFilter(array(
    'type' => 'blah1'
));

echo "Filtered by multiple fields\n";
print_r(array_filter($array, array($filter, 'filter')));

At this point I've given you a great deal of code. I'd suggest that your next step is to combine these two pieces into a single class. This single class would then apply both filtering and sorting together.

2 of 5
1

Do it:

$arr =  array(
  1 => array ( "type" => "blah1", "category" => "cat1", "exp_range" => "this_week" ),
  2 => array ( "type" => "blah1", "category" => "cat2", "exp_range" => "next week" ),
  3 => array ( "type" => "blah1", "category" => "cat1", "exp_range" => "this_week" ),
  4 => array ( "type" => "blah2", "category" => "cat2","exp_range" => "next week" ),
);

function filter(array $arr,array $params){
  $out = array();
  foreach($arr as $key=>$item){
     $diff = array_diff_assoc($item,$params);

     if (count($diff)==1) // if count diff == 1 - Ok
        $out[$key] = $item;
 }
 return $out;
}

$out = filter($arr,array("type" => "blah1", "category" => "cat1"));

echo '<pre>';
print_r($out);
echo '</pre>';

// output

Array
(
  [1] => Array
     (
        [type] => blah1
        [category] => cat1
        [exp_range] => this_week
    )

[3] => Array
    (
        [type] => blah1
        [category] => cat1
        [exp_range] => this_week
    )

)
🌐
Stack Overflow
stackoverflow.com › questions › 35011514 › multidimensional-array-filtering-php
Multidimensional Array Filtering [PHP] - Stack Overflow
And I also tried with ( but if the first one is trance then it doesnt filter out the rest.. even if they are techno ... i need it to filter out at the [5d1838c3aa6344e3109ab4f0122492f7] level ) : $genre = 'trance'; $data = array_filter($dataraw, function($fs) use ($genre) { return current($fs)['channel'] === $genre; });
🌐
GitHub
gist.github.com › trvswgnr › 2487166307ca93bb178e46af1377c356
PHP - Filter keys in multidimensional array · GitHub
PHP - Filter keys in multidimensional array. GitHub Gist: instantly share code, notes, and snippets.
Top answer
1 of 2
5

Of course you can simply loop any given input and check if a given element matches any criteria you implement:

<?php
$input = [
    [
        'id' => 2135,
        'first_name' => 'John',
        'last_name' => 'Doe',
        'gender' => 'male'
    ],
    [
        'id' => 3245,
        'first_name' => 'Sally',
        'last_name' => 'Smith',
        'gender' => 'female'
    ],
    [
        'id' => 5342,
        'first_name' => 'Jane',
        'last_name' => 'Doe',
        'gender' => 'female'
    ],
    [
        'id' => 5623,
        'first_name' => 'Peter',
        'last_name' => 'Doe',
        'gender' => 'male'
    ],
    [
        'id' => 7216,
        'first_name' => 'Mike',
        'last_name' => 'Lill',
        'gender' => 'male'
    ]
];

$needles = ['Doe', 'male'];
$output = [];
array_walk($input, function($element) use ($needles, &$output) {
    $matches = true;
    foreach ($needles as $needle) {
        if (!in_array($needle, $element)) {
            $matches = false;
        }
    }
    if ($matches) {
        $output[] = $element;
    }
});

print_r($output);

The obvious output is:

Array
(
    [0] => Array
        (
            [id] => 2135
            [first_name] => John
            [last_name] => Doe
            [gender] => male
        )

    [1] => Array
        (
            [id] => 5623
            [first_name] => Peter
            [last_name] => Doe
            [gender] => male
        )

)
2 of 2
3

You wish to filter your multidimensional array based on multiple qualifying values irrespective of their keys.

If $datas is coming from a database, you should be performing this filtration in your sql to avoid needlessly squandering resources.

If you must filter in php, then array_filter() is an intuitive and logical function to call upon.

Code: (Demo)

$needles = ['Doe', 'male'];

var_export(
    array_filter($datas, function($row) use ($needles) {
        return !array_diff($needles, $row);
    })
);

array_filter() processes a boolean value in each return as it iterates the rows. array_intersect($needles, $row) will only keep the $needles elements which are present in the current row. If the original $needles contains the same data as the filtered $needles then return true (keep the row).

From PHP7.4 and higher, you can use arrow syntax:

var_export(
    array_filter($datas, fn($row) => !array_diff($needles, $row)
);

Output: (from either snippet)

array (
  0 => 
  array (
    'id' => 2135,
    'first_name' => 'John',
    'last_name' => 'Doe',
    'gender' => 'male',
  ),
  3 => 
  array (
    'id' => 5623,
    'first_name' => 'Peter',
    'last_name' => 'Doe',
    'gender' => 'male',
  ),
)

Note that the original first-level keys are preserved. If your project requires the multidimensional array to be reindexed, write the array_filter() call inside of array_values().


If you'd like some other techniques for the sake of running your own benchmarks with your own data, these language constructs will also provide filtration: (Demo)

foreach ($datas as $row) {
    if (!array_diff($needles, $row)) { // all needles found
        $result[] = $row;
    }
}
var_export($result);  // reindexed already

and

foreach ($datas as $index => $row) {
    if (array_diff($needles, $row)) { // one or more needles not found
        unset($datas[$index]);
    }
}
var_export($datas);

For best efficiency, implement a nested loop with in_array() calls. It may seem counterintuitive to make so many iterated function calls, but the conditional continue allows the time complexity to be optimized and therefore delivers a better "little o" because it can stop iterating as soon as a needle is not found.

Code: (Demo) (array_filter() Demo)

foreach ($datas as $row) {
    foreach ($needles as $needle) {
        if (!in_array($needle, $row)) {
            continue 2;
        }
    }
    $result[] = $row;
}
var_export($result);

I should also point to this related page (which has an unfortunately vague problem statement). I do not endorse a few of the techniques there (including the accepted answer), but there is certainly a technical relationship between these two pages.