PHP
php.net โบ manual โบ en โบ function.array-filter.php
PHP: array_filter - Manual
array_reduce() - Iteratively reduce the array to a single value using a callback function ยท Learn How To Improve This Page โข Submit a Pull Request โข Report a Bug ... If you like me have some trouble understanding example #1 due to the bitwise operator (&) used, here is an explanation.
W3Schools
w3schools.com โบ php โบ func_array_filter.asp
PHP array_filter() Function
Arrays Indexed Arrays Associative Arrays Create Arrays Access Array Items Update Array Items Add Array Items Remove Array Items Sorting 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 Advanced PHP Callback Functions PHP JSON PHP Exceptions
Videos
04:19
How to filter an array in PHP - YouTube
04:26
Getting Used To With array_filter() function - In 4 Minutes - YouTube
01:51
Filtering a PHP Array - YouTube
09:20
PHP Array Filter Function array filter - YouTube
21:39
PHP Higher Order Functions - Array Filter - YouTube
02:58
PHP Array Filtering - YouTube
PHP Tutorial
phptutorial.net โบ home โบ php tutorial โบ php array_filter function
PHP array_filter Function
April 6, 2025 - To pass both the key and value of the element to the callback function, you pass the ARRAY_FILTER_USE_BOTH value as the third argument of the array_filter() function. For example: <?php $inputs = [ 'first' => 'John', 'last' => 'Doe', 'password' => 'secret', 'email' => '' ]; $filtered = array_filter( $inputs, fn ($value, $key) => $value !== '' && $key !== 'password', ARRAY_FILTER_USE_BOTH ); print_r($filtered);Code language: PHP (php)
OnlinePHP
onlinephp.io โบ array-filter โบ manual
array_filter - OnlinePHP.io Example
If no callback is supplied, all empty entries of array will be removed. See empty for how PHP defines empty in this case. ... ARRAY_FILTER_USE_BOTH - pass both value and key as arguments to callback instead of the value Default is 0 which will pass value as the only argument to callback instead.
BCCNsoft
doc.bccnsoft.com โบ docs โบ php-docs-7-en โบ function.array-filter.html
Filters elements of an array using a callback function
... <?php function odd($var) { ... & 1)); } $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $array2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd :\n"; print_r(array_filter($array1, "odd")); echo "Even:\n"; print_r(array_filter($array2, "even")); ?>...
Scaler
scaler.com โบ home โบ topics โบ php array_filter() function
PHP array_filter() Function - Scaler Topics
April 1, 2024 - The array_filter() function in PHP is used to iterate over an array and selectively filter its elements based on a callback function. A callback function is a function that is passed as an argument to another function.
Top answer 1 of 5
3
I would combine array_filter and array_map for this.
Copy$data[] = array("id" => "1", "content" => "Hello");
$data[] = array("id" => "2", "content" => "World");
// filter the data
$data = array_filter($data, fn ($value) => $value['content'] === 'World');
// map the data
$data = array_map(fn ($value) => ['content' => $value['content']], $data);
// reset indexes
$data = array_values($data);
print_r($data);
Example: https://phpize.online/s/9U
2 of 5
0
Everything seems to work fine.
Copy<?php
$data = [];
$data[] = array("id" => "1", "content" => "Hello");
$data[] = array("id" => "2", "content" => "World");
$filtered_data = array_filter($data, function($value) {
return $value['content'] == "World";
});
print_r($filtered_data);
The output is just like expected:
Array ( [1] => Array ( [id] => 2 [content] => World ) )
But if you want to leave only some fields in resulting array, array_filter will not help you (at least without a crutch).
You may want to iterate source array and filter it by yourself.
Copy<?php
$data = [];
$data[] = array("id" => "1", "content" => "Hello");
$data[] = array("id" => "2", "content" => "World");
$filtered_data = [];
foreach($data as $v) {
if($v['content'] == "World")
$filtered_data[] = ["content" => $v['content']];
}
print_r($filtered_data);
The output then would be:
Array ( [0] => Array ( [content] => World ) )
Tutorialspoint
tutorialspoint.com โบ php โบ php_function_array_filter.htm
PHP - Function array_filter()
... <?php function odd($var) { ... & 1)); } $input1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5); $input2 = array(6, 7, 8, 9, 10, 11, 12); echo "Odd Values:\n"; print_r(array_filter($input1, "odd")); echo "Even Values:\n"; print_r(array_filter($input2, "even")); ?>...
ZetCode
zetcode.com โบ php-array โบ array-filter
PHP array_filter - Array Filtering in PHP
This example filters out all odd numbers from an array, keeping only evens. ... <?php $numbers = [1, 2, 3, 4, 5, 6]; $evenNumbers = array_filter($numbers, function($n) { return $n % 2 === 0; }); print_r($evenNumbers);
Top answer 1 of 6
161
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 ($var) use ($filterBy) {
return ($var['name'] == $filterBy);
});
2 of 6
5
If you want to make this a generic function use this:
function filterArrayByKeyValue($array, $key, $keyValue)
{
return array_filter($array, function($value) use ($key, $keyValue) {
return $value[$key] == $keyValue;
});
}