PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

Iterating containers

Zend_Navigation_Container implements RecursiveIteratorIterator, and can be iterated using any Iterator class. To iterate a container recursively, use the RecursiveIteratorIterator class.

Example 638. Iterating a container

<?php
/*
 * Create a container from an array
 */
$container = new Zend_Navigation(array(
    array(
        
'label' => 'Page 1',
        
'uri'   => '#'
    
),
    array(
        
'label' => 'Page 2',
        
'uri'   => '#',
        
'pages' => array(
            array(
                
'label' => 'Page 2.1',
                
'uri'   => '#'
            
),
            array(
                
'label' => 'Page 2.2',
                
'uri'   => '#'
            
)
        )
    ),
    array(
        
'label' => 'Page 3',
        
'uri'   => '#'
    
)
));

// Iterate flat using regular foreach:
// Output: Page 1, Page 2, Page 3
foreach ($container as $page) {
    echo 
$page->label;
}

// Iterate recursively using RecursiveIteratorIterator
$it = new RecursiveIteratorIterator(
        
$containerRecursiveIteratorIterator::SELF_FIRST);

// Output: Page 1, Page 2, Page 2.1, Page 2.2, Page 3
foreach ($it as $page) {
    echo 
$page->label;
}

Zend Framework