如何获取php中第一次出现的array_walk_recursive的值

I have a deep multidimensional array that I am needing to extract the value of a specific key. I have found that the array_walk_recursive function will be my best option. I only need the first occurrence.

My array looks like this - (except much more complicated)

Array (
    [vehicle info] => Array (
        [one] => Array (
            [submodel] => LX
            [engine] => 2.3
        )
        [two] => Array (
            [color] => blue
            [year] => 2007
            [wheels] => 4
        )
        [three] => Array (
            [submodel] => LX
            [make] => Ford
            [model] => F-150
            [offroad] => No
        )
    )
)

The issue here is, submodel is in both one and three. Additionally, the array is not consistent, so I must use array_walk_recursive to search through it for the matching key, then return the value for that key.

Here is my current code -

array_walk_recursive ($array, (function ($item, $key) {
    $wanted = "submodel";
    if ($key === $wanted) {
        echo ("$key is $item");
    }
}));

The above returns submodel is LXsubmodel is LX.

Bonus Question!! How can I search for multiple keys and return the first corresponding value for each of those? I was thinking putting all wanted keys in an array, then do a foreach loop, but don't quite know how to structure this. I am new to php.

Code:

$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']
    ]
];

$find=['submodel','offroad'];

array_walk_recursive($array,function($v,$k)use($find,&$result){
    if(in_array($k,$find) && !isset($result[$k])){
        $result[$k]="$k is $v";
    }
});
var_export($result);

Output:

array (
  'submodel' => 'submodel is LX',
  'offroad' => 'offroad is No',
)

I would start by setting the values you want to null, and then only saving them if they haven't been found yet, by checking is_null(). I haven't tested this code, but it should look something like this:

$submodel = null;
array_walk_recursive ($array, (function ($item, $key) {
    $wanted = "submodel";
    if ($key === $wanted && is_null($submodel)) {
        echo ("$key is $item");
        $submodel = $item;
    }
}));

array_walk_recursive() has the defect of not allowing to return matching results however in PHP 7 you could use an anonymous function and a variable to store the matching value.

$matching = null;
$wanted = "submodel";

array_walk_recursive ($array, function ($item, $key) use ($wanted, $matching) {
    if (($key === $wanted) && is_null($matching)) {
        $matching = $item;
    }
});

As far as there is no way to return early from array_walk_recursive(), I'd suggest to create a function to find the first occurrence of $wanted:

$arr = [
  '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'],
    ],
];

function find($needle, $haystack, $found = '')
{
    foreach ($haystack as $key => $value) {
        if ($found) {
            break;
        }
        if ($key === $needle) {
            $found = "{$needle} is {$value}";
            break;
        }
        if (is_array($value)) {
            $found = find($needle, $value, $found);
        }
    }
    return $found;
}

$wanted = 'submodel';
$result = find($wanted, $arr);

var_dump($result); // string(14) "submodel is LX"

Live demo


Update: to search for multiple keys you'll need to do it in a loop:

$multiple_keys = array('submodel', 'year');

foreach ($multiple_keys as $wanted) {
    var_dump(find($wanted, $arr));
}

// Output:
//    string(14) "submodel is LX"
//    string(12) "year is 2007"

Live demo