I'm trying to filter my first array by my second array. I want to filter every person whose age is 22,25,35, or 40. I have these whitelisted values store in an $a2
$a1 = [
['name' => 'mike', 'age' => 18],
['name' => 'james', 'age' => 22],
['name' => 'sarah', 'age' => 35],
['name' => 'ken', 'age' => 29],
];
$a2 = [22, 25, 35, 40];
I tried array_intersect() like:
$results = array_intersect($a2, $a1['age']);
var_dump($results);
And array_filter() but without success.
My desired output is:
[
['name' => 'james', 'age' => 22],
['name' => 'sarah', 'age' => 35]
]
array_intersect and array_diff don't work well with multi dimensional arrays.
try this and see how work:
the result should be [2]. if you work with multidimensional arrays and you only interesting in one column of arrays do first:
and then