我有一个数组这样
$foo = array();
$foo['/'] = 'value';
$foo['/foo'] = 'value';
$foo['/foo/bar'] = 'value';
$foo['/test'] = 'value';
$foo['/test/tester'] = 'value';
$foo['/hello'] = 'value';
$foo['/hello/world'] = 'value';
$foo['/hello/world/blah'] = 'value';
我需要做的是存储在这些子页面树形结构,所以需要自动转换为:
$foo = array(
'/' => array(
'value' => 'value',
'children' => array(
'/foo' => array(
'value' => 'value',
'children' => array(
'/foo/bar' => array(
'value' => 'value',
'children' => array()
);
我想我会做,是这样的:
$newArray = array();
foreach($foo as $key => $val)
{
$bits = explode('/', $key);
foreach($bits as $bit)
{
$newArray[$bit] = array('val' => $val);
}
}
print_r($newArray);
除了不知何故,我需要去到newArray,并跟踪我到数组有多深。有没有人有他们如何做的例子脚本,或者有任何时髦的数组步行技巧来做到这一点?
请去了解递归。 – blockhead
@blockhead:http://stackoverflow.com/a/23882 –