0

For example, I have an array:

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),
);

How generate nested array without using recursion (via Zend_Navigation) to get this one?

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 Answer 1

3

try

$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());
Sign up to request clarification or add additional context in comments.

2 Comments

Excellent! I just added controller name (or action or module) as a parameter to Zend_Navigation_Page::factory to avoid error. Thanks a lot!
@SMka - This would only work if the children were 'one level deep'. Is there any way to expand on it so it would work to the 'nth' degree?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.