0

我试图获取一个数组并与父母和孩子一起制作一个新的多维。我想我可以靠近,但看起来还是不对。

$value->menu_item_parent //Contains the parent value that can be used for key

我错过了什么?

$the_mega = array();
foreach ($menuitems as $key => $value) {
    if ($value->menu_item_parent=='0') {
        $the_mega["parents"][]=$value->title;
    }else{
        $the_mega[$value->title]["childs"]=$value->title;
    }       
}
}
echo '<pre>';
print_r($the_mega);
echo '</pre>';

 Array
(
    [parents] => Array
        (
            [0] => Parent 1
            [1] => Parent 2
        )

    [Child 1] => Array
        (
            [childs] => Child 1
        )

    [Child 2] => Array
        (
            [childs] => Child 2
        )

    [Child 3] => Array
        (
            [childs] => Child 3
        )

)
4

1 回答 1

0

尝试这个:

$the_mega = array();
foreach ($menuitems as $key => $value) {
    if ($value->menu_item_parent=='0') {
        $the_mega[$key]['parents'][]=$value->title;
    }else{
        $the_mega[$value->title]['childs'][]=$value->title;
    }       
}
于 2013-08-06T13:55:52.047 回答