想象一下以下多维数组:
$a = array(
'key' => 'hello',
'children' => array(
array(
'key' => 'sub-1'
),
array(
'key' => 'sub-2',
'children' => array(
array(
'key' => 'sub-sub-1'
)
)
)
)
);
我需要一个函数,它递归地运行这样一个数组,然后使用粘合字符串最终返回某个子键的所有值的链。
function collectKeyChain(array $array, $key, $parentKey, $glue){
foreach($array as $k => $v){
if(is_array($v[$parentKey]))
$children=self::collectKeyChain($v[$parentKey], $key, $parentKey, $glue, $out);
$chain[]=$glue . implode($glue, $children);
}
return $chain;
}
这样调用:
collectValueChain($a, 'key', 'children', '/');
然后应该返回这个:
array(
'hello',
'hello/sub-1',
'hello/sub-2',
'hello/sub-2/sub-sub-1'
)
不幸的是,我的大脑似乎完全无法完成"嵌套思维"的任务。上面函数中提供的代码不起作用,只是因为它毫无意义。我可以使用递归函数返回数组或字符串。但在最终输出中,我需要一个数组。另一方面,我需要把这些元素连在一起。
这就是困境。我脑海中出现的唯一解决方案是使用另一个参数,它是通过引用传递的,这是一个充满结果的数组。
像这样:
collectValueChain($a, 'key', 'children', '/', $arrayToBeFilledWithResults);
但如果不使用多个函数,我甚至无法完成这项工作。
也许这是不可能更容易做到的,但我仍然想知道。
试试这个:
function collectKeyChain(array $array, $key, $parentKey, $glue) {
$return = array();
foreach ($array as $k => $v) {
if ($k == $key) {
$base = $v;
$return[] = $base;
} elseif ($k == $parentKey && is_array($v)) {
foreach ($v as $_v) {
$children = collectKeyChain($_v, $key, $parentKey, $glue);
foreach ($children as $child) {
$return[] = $base . $glue . $child;
}
}
}
}
return $return;
}
请注意,如果这是类中的静态方法,则必须将self::添加到递归方法调用中。
一个更简单的版本,没有很多foreach。考虑第二种方法:
collectValueChain($a, 'key', 'children', '/', $arrayToBeFilledWithResults);
我这样做:
function collectValueChain($a, $keyname, $parent, $glue, &$rtn, $pre="") {
$_pre = "";
if ($a[$keyname]) {
$rtn[] = $_pre = $pre.$glue.$a[$keyname];
}
if ($a[$parent]) {
if(is_array($a[$parent])) {
foreach($a[$parent] as $c)
collectValueChain($c, $keyname, $parent, $glue, $rtn, $_pre );
} else {
collectValueChain(a[$parent], $keyname, $parent, $glue, $rtn, $_pre );
}
}
$qtd = count($rtn);
return $rtn[-1];
}