<?php
$array = [
'vehicle info' => [
'one' => [
'submodel' => 'LX',
'engine' => 2.3
],
'two' => [
'color' => 'blue',
'year' => 2007,
'wheels' => 4
],
'three' => [
'submodel' => 'LX',
'make' => 'Ford',
'model' => 'F-150',
'offroad' => 'No'
]
]
];
$soughtKeys = array_flip(['submodel', 'offroad']);
function earlyReturningRecursion(array $array, array $soughtKeys, array &$result = []): array
{
foreach ($array as $key => $value) {
if (!array_diff_key($soughtKeys, $result)) {
return $result;
} elseif (is_array($value)) {
earlyReturningRecursion($value, $soughtKeys, $result);
} elseif (isset($soughtKeys[$key]) && !isset($result[$key])) {
$result[$key] = "$key is $value";
}
}
return $result;
}
var_export(earlyReturningRecursion($array, $soughtKeys));
preferences:
27.52 ms | 406 KiB | 5 Q