阅读这个问题Merge and group by several arrays我得到了以下想法:当使用多级数组,可能重复键时,有一个函数可以迭代这样的数组,因为它是平的,如
foreach(flatten($deepArray) as $key => $val)....
如何撰写flatten()
的任何想法?有没有标准的解决方案?
(请注意,由于重复键,flatten()
不能简单地返回新数组。)
答案 0 :(得分:13)
$array = array(
0 => 'a',
1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))),
2 => 'b',
3 => array('subA','subB','subC'),
4 => 'c'
);
foreach (return new RecursiveIteratorIterator(new RecursiveArrayIterator($array))
as $key => $val) {
printf(
'%s: %s' . "\n",
$key, $val
);
}
/* Output:
0: a
0: subA
1: subB
0: subsubA
1: subsubB
0: deepA
1: deepB
2: b
0: subA
1: subB
2: subC
4: c
*/
扩展RecursiveIteratorIterator
以返回当前的密钥堆栈
class MyRecursiveIteratorIterator extends RecursiveIteratorIterator
{
public function key() {
return json_encode($this->getKeyStack());
}
public function getKeyStack() {
$result = array();
for ($depth = 0, $lim = $this->getDepth(); $depth < $lim; $depth += 1) {
$result[] = $this->getSubIterator($depth)->key();
}
$result[] = parent::key();
return $result;
}
}
foreach ($it = new MyRecursiveIteratorIterator(new RecursiveArrayIterator($array))
as $key => $val) {
printf('%s (%s): %s' . "\n", implode('.', $it->getKeyStack()), $key, $val);
}
/* Output:
0 ([0]): a
1.0 ([1,0]): subA
1.1 ([1,1]): subB
1.2.0 ([1,2,0]): subsubA
1.2.1 ([1,2,1]): subsubB
1.2.2.0 ([1,2,2,0]): deepA
1.2.2.1 ([1,2,2,1]): deepB
2 ([2]): b
3.0 ([3,0]): subA
3.1 ([3,1]): subB
3.2 ([3,2]): subC
4 ([4]): c
*/
又一个版本,这次没有使用RecursiveArrayIterator:
function flatten(array $array = array(), $keyStack = array(), $result = array()) {
foreach ($array as $key => $value) {
$keyStack[] = $key;
if (is_array($value)) {
$result = flatten($value, $keyStack, $result);
}
else {
$result[] = array(
'keys' => $keyStack,
'value' => $value
);
}
array_pop($keyStack);
}
return $result;
}
foreach (flatten($array) as $element) {
printf(
'%s: %s (depth: %s)' . "\n",
implode('.', $element['keys']),
$element['value'],
sizeof($element['keys'])
);
}
/*
0: a (depth: 1)
1.0: subA (depth: 2)
1.1: subB (depth: 2)
1.2.0: subsubA (depth: 3)
1.2.1: subsubB (depth: 3)
1.2.2.0: deepA (depth: 4)
1.2.2.1: deepB (depth: 4)
2: b (depth: 1)
3.0: subA (depth: 2)
3.1: subB (depth: 2)
3.2: subC (depth: 2)
4: c (depth: 1)
*/
答案 1 :(得分:1)
您还可以编写一个简单的遍历函数:
function flatten($node, $fn, $keys = array()) {
if (! is_array($node)) {
$fn($node, $keys);
} else {
foreach ($node as $k => $v) {
$new_keys = $keys;
$new_keys[] = $k;
flatten($v, $fn, $new_keys);
}
}
}
$array = array(
0 => 'a',
1 => array('subA','subB',array(0 => 'subsubA', 1 => 'subsubB', 2 => array(0 => 'deepA', 1 => 'deepB'))),
2 => 'b',
3 => array('subA','subB','subC'),
4 => 'c'
);
// will output: a subA subB subsubA subsubB deepA deepB b subA subB subC c
flatten($array, function($v, $k) {
echo $v . ' ';
});
如果您不希望每次使用其他函数作为参数调用它,我还编写了一个将返回数组的适配器:
function flatten_array($node) {
$acc = array();
flatten($node, function($node, $keys) use (&$acc) {
$acc[implode('.', $keys)] = $node;
});
return $acc;
}
// will spit out the same output as that in Yoshi's answer:
foreach (flatten_array($array) as $k => $v) {
echo $k .' => ' . $v . "\n";
}
注意:
call_user_func
调用它们。