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

How do I get the value of the first occurrence of array_walk_recursive in php

我有一个深度多维数组,需要提取特定键的值。我发现 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 在其中搜索匹配的键,然后 return 该键的值。

这是我当前的代码 -

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

以上returns submodel is LXsubmodel is LX.

奖金问题!! 如何搜索多个键和 return 每个键的第一个对应值?我想把所有想要的键放在一个数组中,然后做一个 foreach 循环,但不太清楚如何构造它。我是 php.

的新手

我会首先将您想要的值设置为 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;
    }
}));

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

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

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

至于无法从 array_walk_recursive() 提早 return,我建议创建一个函数来查找第一次出现的 $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

array_walk_recursive() 是为此任务调用的适当本机函数。跟踪哪些键已经在结果数组中声明,并确保它们永远不会被覆盖。

代码:(Demo)

$needles = ['submodel', 'offroad'];
$result = [];
array_walk_recursive(
    $array,
    function($value, $key) use ($needles, &$result) {
        if (
            in_array($key, $needles)
            && !isset($result[$key])
        ) {
            $result[$key] = "$key is $value";
        }
    }
);
var_export($result);

输出:

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

如果您的应用程序存在性能问题,那么本机函数就会变得不那么吸引人,因为它总是会迭代整个输入数组的结构——即使在遇到所有查找的键之后也是如此。如果你想“提前中断”(短路),那么你需要设计自己的递归函数,当找到所有寻找的键时,它将 return。

代码:(Demo)

$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)) {  // check if result is complete
            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));
// same output as the first snippet