我今天一直在处理一些代码,我遇到了一个小的多维数组问题。首先,阅读我编写的一些代码以便更好地了解问题本身可能很方便:
public function treeLeaves(array $elements, $parent = 0) {
$branch = array();
foreach($elements as $element) {
$leaf = array('pageid' => $element['pageid'],
'page_parent' => $element['page_parent'],
'label' => ucfirst($element['page_print'][0]['print_title']),
'uri' => $element['page_alias']);
if($element['page_parent'] == $parent) {
$children = $this->treeLeaves($elements, $leaf['pageid']);
if($children) {
foreach($children as $key => $child) {
$leaf['pages'][] = $children[$key];
}
}
$branch[] = $leaf;
}
}
return $branch; }
出于某种原因,我无法弄清楚如何将父URI别名粘贴到所有单独的子URI上。我想要的结果应该是这样的:http://pastebin.com/Eh9ExBjG
我希望有些高手可以帮助我。我一直在尝试这么多不同的东西,但不能解决这个问题,即使我觉得它相对容易解决。
答案 0 :(得分:1)
有些简化,但我想你会明白这个想法:
function treeLeaves($elements, $parent = 0, $baseUri = '/index') {
$branch = array();
foreach($elements as $element) {
if ($element['page_parent'] == $parent) {
$leaf = array(
'uri' => $baseUri . '/' . $element['page_alias'];
);
$leaf['pages'] = treeLeaves($elements, $element['pageid'], $leaf['uri']);
$branch[] = $leaf;
}
}
return $branch;
}