Zend Framework:是否可以从列表数组创建嵌套数组?

时间:2012-03-16 09:19:53

标签: arrays zend-framework nested zend-navigation

例如,我有一个数组:

array(
   array('id' => 1, 'parent' => 0),
   array('id' => 5, 'parent' => 0),
   array('id' => 3, 'parent' => 4),
   array('id' => 4, 'parent' => 6),
   array('id' => 6, 'parent' => 1),
);

如何生成嵌套数组而不使用递归(通过Zend_Navigation)来获取这个?

array(
    'id' => 0,
    'pages' => array(
                     array('id => 1,
                           'pages' => array('id' => 6, 
                                            'pages' => array('id' => 4,
                                                             'pages' => array('id'=>3, 'pages' => null)
                                                             )
                                             )
                            )
                     ),
               array(
                      array('id' => 5, 'pages => null)
               )
     )

1 个答案:

答案 0 :(得分:3)

$pages = array(
   array('id' => 1, 'parent' => 0),
   array('id' => 5, 'parent' => 0),
   array('id' => 3, 'parent' => 4),
   array('id' => 4, 'parent' => 6),
   array('id' => 6, 'parent' => 1),
);
$container = new Zend_Navigation();
$container->addPage(new Zend_Navigation_Page_Uri(array(
    'label' => 'root',
    'uri' => '',
    'visible' => false,
    'id' => 0,
)));

foreach ($pages as $page) {
    $found = $container->findById($page['parent']);
    $found->addPage(
        Zend_Navigation_Page::factory(array(
            'id' => $page['id'],
            'label' => 'label' . $page['id']
        )));
}
var_dump($container->toArray());