I want to search through a 2-dimensional array, but I only want to search in a specific field in the 2nd Dimension. If found, I would like to return the Key. No need to go on from there, I only need the first occurence but I wouldn't mind to get all occurences either.
The Array might look like this:
$array = [
0 => ['value' => 'x', 'foo' => 'bar'],
1 => ['value' => 'y', 'foo' => 'bar'],
2 => ['value' => 'z', 'foo' => 'x'],
];
Now my first thought would be something like this:
function myCustomArraySearch($array, $searchkey, $searchvalue) {
foreach ($array as $key => $value) {
if ($value[$searchkey] == $searchvalue) {
return $key;
}
}
return false;
}
echo myCustomArraySearch($array, 'value', 'x');
I'm sure, there is a more elegnt solution. Any ideas?
array_walk_recursivebuilt-in php function php.net/manual/ro/function.array-walk-recursive.php