array_filter in laravel code example
Example 1: array filter use key
$my_array = ['foo' => 1, 'hello' => 'world'];
$allowed = ['foo', 'bar'];
$filtered = array_filter(
$my_array,
function ($key) use ($allowed) {
return in_array($key, $allowed);
},
ARRAY_FILTER_USE_KEY
);
Example 2: php array filter syntax
$numbers = [2, 4, 6, 8, 10];
function MyFunction($number)
{
return $number > 5;
}
$filteredArray = array_filter($numbers, "MyFunction");
Example 3: how can use filter in php7.2
Odd :
Array
(
[a] => 1
[c] => 3
[e] => 5
)
Even:
Array
(
[0] => 6
[2] => 9
[4] => 10
[6] => 12
)
Example 4: filter value in array php return single value
?php
$data= [
0 => [1, 'test1'],
1 => [2, 'test2'],
2 => [3, 'test3'],
];
$ids = array_map(function($item) {
return $item[0];
}, $data);
var_dump($ids);