Luckily, the array_filter function not only has the default functionality to filter out everything that converts to a Boolean false value, but it offers a very helpful callback function. This means we can essentially do whatever the mind can see with the data at hand.
In this tutorial, filtering by either of the key or value will be explored.
The Array
To start, we need some data, so I knocked up this pretty small multi-dimensional array, see below –
$array["PHPDevelopers"] = [
["Name" => 'Dan Englishby', 'Age' => '99', 'DeveloperLevel' => '3', 'Email' => '[email protected]', 'Gender' => 'Male'],
["Name" => 'Del Smith', 'Age' => '33', 'DeveloperLevel' => '8', 'Email' => '[email protected]', 'Gender' => 'Male'],
["Name" => 'Jame Raphael', 'Age' => '27', 'DeveloperLevel' => '4', 'Email' => '[email protected]', 'Gender' => 'Male'],
["Name" => 'Jayna Honaker', 'Age' => '42', 'DeveloperLevel' => '9', 'Email' => '[email protected]', 'Gender' => 'Female'],
["Name" => 'Stephine Ransome', 'Age' => '42', 'DeveloperLevel' => '4', 'Email' => '[email protected]', 'Gender' => 'Female'],
];
$array["C#Developers"] = [
["Name" => 'Craig Woolard', 'Age' => '22', 'DeveloperLevel' => '4', 'Email' => '[email protected]', 'Gender' => 'Male'],
["Name" => 'Tiffiny Minich', 'Age' => '21', 'DeveloperLevel' => '3', 'Email' => '[email protected]', 'Gender' => 'Female'],
["Name" => 'Chad Lanser', 'Age' => '55', 'DeveloperLevel' => '7', 'Email' => '[email protected]', 'Gender' => 'Male'],
["Name" => 'Latisha Langlais', 'Age' => '25', 'DeveloperLevel' => '2', 'Email' => '[email protected]', 'Gender' => 'Female'],
["Name" => 'Dave Gaona', 'Age' => '63', 'DeveloperLevel' => '9', 'Email' => '[email protected]', 'Gender' => 'Male'],
];
And so we know what it looks like in a bit of a more readable fashion, see below after a var_dump is executed on it –
array (
'PHPDevelopers' =>
array (
0 =>
array (
'Name' => 'Dan Englishby',
'Age' => '99',
'DeveloperLevel' => '3',
'Email' => '[email protected]',
'Gender' => 'Male',
),
1 =>
array (
'Name' => 'Del Smith',
'Age' => '33',
'DeveloperLevel' => '8',
'Email' => '[email protected]',
'Gender' => 'Male',
),
2 =>
array (
'Name' => 'Jame Raphael',
'Age' => '27',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
'Gender' => 'Male',
),
3 =>
array (
'Name' => 'Jayna Honaker',
'Age' => '42',
'DeveloperLevel' => '9',
'Email' => '[email protected]',
'Gender' => 'Female',
),
4 =>
array (
'Name' => 'Stephine Ransome',
'Age' => '42',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
'Gender' => 'Female',
),
),
'C#Developers' =>
array (
0 =>
array (
'Name' => 'Craig Woolard',
'Age' => '22',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
'Gender' => 'Male',
),
1 =>
array (
'Name' => 'Tiffiny Minich',
'Age' => '21',
'DeveloperLevel' => '3',
'Email' => '[email protected]',
'Gender' => 'Female',
),
2 =>
array (
'Name' => 'Chad Lanser',
'Age' => '55',
'DeveloperLevel' => '7',
'Email' => '[email protected]',
'Gender' => 'Male',
),
3 =>
array (
'Name' => 'Latisha Langlais',
'Age' => '25',
'DeveloperLevel' => '2',
'Email' => '[email protected]',
'Gender' => 'Female',
),
4 =>
array (
'Name' => 'Dave Gaona',
'Age' => '63',
'DeveloperLevel' => '9',
'Email' => '[email protected]',
'Gender' => 'Male',
),
),
);
Filter Multidimensional Array By Key
So, we’ve got our array, now let’s filter it by the Key! In the array I made earlier, there are 2 main keys, ‘PHPDevelopers’ & ‘C#Developers’, so let’s say we only want the PHPDevelopers data for this example.
Note: When using the array_filter callback functionality, by default, will only pass in the array’s values. You must explicitly specify that you want to pass the keys in, either on their own or with the values too.
The constants to pass in as a parameter are of the following –
ARRAY_FILTER_USE_KEYARRAY_FILTER_USE_BOTH
So, with this in mind, take a look at the following function to filter the array by the PHPDevelopers only.
Filtering the array by Key
$phpDevs = array_filter($array, function ($key) {
return $key == 'PHPDevelopers';
}, ARRAY_FILTER_USE_KEY); // Alternatively, use ARRAY_FILTER_USE_BOTH flag.
And the output below –
array (
'PHPDevelopers' =>
array (
0 =>
array (
'Name' => 'Dan Englishby',
'Age' => '99',
'DeveloperLevel' => '3',
'Email' => '[email protected]',
'Gender' => 'Male',
),
1 =>
array (
'Name' => 'Del Smith',
'Age' => '33',
'DeveloperLevel' => '8',
'Email' => '[email protected]',
),
'Gender' => 'Female',
2 =>
array (
'Name' => 'Jame Raphael',
'Age' => '27',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
),
3 =>
array (
'Name' => 'Jayna Honaker',
'Age' => '42',
'DeveloperLevel' => '9',
'Email' => '[email protected]',
),
4 =>
array (
'Name' => 'Stephine Ransome',
'Age' => '42',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
'Gender' => 'Female',
),
),
);
What happens if we execute the filter without the optional flag parameter?
Whilst we are on the key subject, let’s take a look at what would happen if we take away the key flag.
$phpDevs = array_filter($array, function ($key) {
return $key == 'PHPDevelopers';
});
This would output sweet nothings as below, so always be sure to utilize the flag when necessary.
array (
);
Filter Multidimensional Array By Value
With the array earlier, let’s say we want to filter the PHP Developers who are over the age of 40. Have a look at the following code.
Note:
$valueis each array instance of the PHPDevelopers array- Using the
usecommand allows us to ‘import’ an extra variable which is not within the current scope. - We specify the key within the first
array_filterparameter so that we pass in only the PHPDevelopers group.
$age = 40;
$phpDevsOver40 = array_filter($array["PHPDevelopers"], function ($value) use ($age) {
return ($value["Age"] > $age);
});
This would yield the following output, which is 3 records, all of which are aged over 40. –
array (
0 =>
array (
'Name' => 'Dan Englishby',
'Age' => '99',
'DeveloperLevel' => '3',
'Email' => '[email protected]',
'Gender' => 'Male',
),
3 =>
array (
'Name' => 'Jayna Honaker',
'Age' => '42',
'DeveloperLevel' => '9',
'Email' => '[email protected]',
'Gender' => 'Female',
),
4 =>
array (
'Name' => 'Stephine Ransome',
'Age' => '42',
'DeveloperLevel' => '4',
'Email' => '[email protected]',
'Gender' => 'Female',
),
);
By utilizing the use statement we can pass in a custom parameter, in this case of which it’s essentially a search term. We search the array and filter any records that have an age greater than 40.
Summary
As you can see from this quick tutorial, array_filter has unlimited use cases, whether it be for general data crunching or even filter products a user has selected on the front end of a shops website! You can check out the full documentation of array_filter here.