0

我今天一直在编写一些代码,但遇到了一个多维数组问题。首先,阅读我编写的一些代码以更好地了解问题本身可能会很方便:

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

希望有高手能帮帮我。我一直在尝试很多不同的东西,但无法弄清楚这件事,即使我觉得它相对容易解决。

4

1 回答 1

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;
}
于 2013-11-02T19:45:56.003 回答