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

时间:2017-06-05 19:38:00

标签: php array-walk

我有一个深度多维数组,我需要提取特定键的值。我发现array_walk_recursive函数将是我的最佳选择。我只需要第一次出现。

我的数组看起来像这样 - (除了更复杂的)

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
        )
    )
)

这里的问题是,submodel同时包含一个和三个。此外,数组不一致,因此我必须使用array_walk_recursive在其中搜索匹配的键,然后返回该键的值。

这是我目前的代码 -

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

上述内容会返回submodel is LXsubmodel is LX

奖金问题!! 如何搜索多个键并返回每个键的第一个相应值?我想把所有想要的键放在一个数组中,然后做一个foreach循环,但不太知道如何构造它。我是php的新手。

4 个答案:

答案 0 :(得分:2)

我首先要设置您想要的值null,然后只检查is_null(),只保存尚未找到的值。我没有测试过这段代码,但看起来应该是这样的:

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

答案 1 :(得分:2)

array_walk_recursive()具有不允许返回匹配结果的缺陷,但是在PHP 7中,您可以使用匿名函数和变量来存储匹配值。

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

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

答案 2 :(得分:2)

如果无法从array_walk_recursive()提前返回,我建议创建一个函数来查找第一次出现的$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"

直播demo

更新:搜索您需要在循环中执行的多个键:

$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"

直播demo

答案 3 :(得分:2)

代码:

$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);

输出:

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