我有一个深层的多维数组,我需要提取特定键的值。我发现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循环,但不太知道如何构建它。我是新人。
array_walk_recursive()
是呼吁此任务的适当本机函数。跟踪结果数组中已经声明了哪些键,并确保它们永远不会被覆盖。
代码:(演示(
$needles = ['submodel', 'offroad'];
$result = [];
array_walk_recursive(
$array,
function($value, $key) use ($needles, &$result) {
if (
in_array($key, $needles)
&& !key_exists($key, $result)
) {
$result[$key] = "$key is $value";
}
}
);
var_export($result);
输出:
array (
'submodel' => 'submodel is LX',
'offroad' => 'offroad is No',
)
为了提高性能,只需要进行第一个资格赛,请将return;
作为if
块内部的最后一行。
另外,您可以设计自己的递归功能,当找到所有寻求的钥匙时,该功能将返回。
代码:(演示(
$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]) && !key_exists($key, $result)) {
$result[$key] = "$key is $value";
}
}
return $result;
}
var_export(earlyReturningRecursion($array, $soughtKeys));
// same output as the first snippet
我将首先设置要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((的缺陷是不允许返回匹配结果,但是在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()
提早返回,我建议创建一个函数以查找$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"
实时演示
更新:要搜索多个键,您需要在循环中进行操作:
$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"
实时演示