How to Filter Multidimensional Array by Key Value in PHP

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.

In the following example code, we will show you how to filter values from multidimensional array similar to SQL LIKE using array_filter() function in PHP. Using this code, you can filter multidimensional array by specific key and value.

$array = array(
    array(
'name' => 'John Doe''email' => '[email protected]'),
    array(
'name' => 'Marry Lies''email' => '[email protected]'),
    array(
'name' => 'Andrew Joe''email' => '[email protected]'),
);

$like 'jo';

$result array_filter($array, function ($item) use ($like) {
    if (
stripos($item['name'], $like) !== false) {
        return 
true;
    }
    return 
false;
});

The above code returns the filtered array.

Array
(
    [0] => Array
        (
            [name] => John Doe
            [email] => [email protected]
        )

    [2] => Array
        (
            [name] => Andrew Joe
            [email] => [email protected]
        )

)

Related Articles

Comments 0