I would rather filter it anther way such as this:
$a = array (
8708 =>
array (
'ref_id' => 93939
),
8709 =>
array (
'ref_id' => 116234
),
8710 =>
array (
'ref_id' => 116234
)
);
$data['attr'][8] = '116234';
$filtered = array_intersect(array_combine(array_keys($a),array_column($a,'ref_id')),[$data['attr'][8]]);
print_r($filtered);
Output
Array
(
[8709] => 116234
[8710] => 116234
)
Sandbox
But I am probably just being weird.
Oh and as a Bonus this also makes it possible to match multiple values, where the filter method was limited to one (use ($data)
and dependent condition). The last array [$data['attr'][8]]
can have multiple values to match with. In this case it's [116234]
, but it could just as easily be this [116234,93939]
which would return the full array.
To explain how it works:
$filtered = array_intersect(
array_combine(
array_keys($a),
array_column($a, 'ref_id')
),
[$data['attr'][8]]
);
Will work our way through it:
array_column($a, 'ref_id')
//Output
Array
(
[0] => 93939
[1] => 116234
[2] => 116234
)
This flattens out our array, to make it easier to work with. Next we combine that with the array keys of the original array:
array_keys($a)
//Output
Array
(
[0] => 8708
[1] => 8709
[2] => 8710
)
This is an important step, because both arrays are the same size, same order, we can combine them and basically un-nest the ref_id
column.
array_combine(
array_keys($a),
array_column($a, 'ref_id')
),
//Output
Array
(
[8708] => 93939
[8709] => 116234
[8710] => 116234
)
Now it's a trivial matter of using array intersect to retrieve the parts we want.
If you wanted to retrieve just the ID's such as this output:
Array
(
[0] => 8709
[1] => 8710
)
This can be done by wrapping the whole thing in another array keys, like this:
$filtered = array_keys(array_intersect(array_combine(array_keys($a),array_column($a,'ref_id')),[$data['attr'][8]]));
And the last parting gift I will give is this:
function filter_mutidimensional(array $array, $match){
if(!is_array($match)) $match = [$match];
return array_keys(array_intersect(array_combine(array_keys($array),array_column($array,'ref_id')),$match));
}
//which you can call like
filter_mutidimensional($a,$data['attr'][8]);
//or
filter_mutidimensional($a,[$data['attr'][8]]);
//or
filter_mutidimensional($a,[$data['attr'][8],93939]);
Hope that all makes sense!