Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>You can use RecursiveCachingIterator to do what you want. Here is an example, (source: <a href="https://github.com/cballou/PHP-SPL-Iterator-Interface-Examples/blob/master/recursive-caching-iterator.php" rel="nofollow">https://github.com/cballou/PHP-SPL-Iterator-Interface-Examples/blob/master/recursive-caching-iterator.php</a>)</p> <pre><code>&lt;?php // example navigation array $nav = array( 'Home' =&gt; '/home', 'Fake' =&gt; array( 'Double Fake' =&gt; array( 'Nested Double Fake' =&gt; '/fake/double/nested', 'Doubly Nested Double Fake' =&gt; '/fake/double/doubly' ), 'Triple Fake' =&gt; '/fake/tripe' ), 'Products' =&gt; array( 'Product 1' =&gt; '/products/1', 'Product 2' =&gt; '/products/2', 'Product 3' =&gt; '/products/3', 'Nested Product' =&gt; array( 'Nested 1' =&gt; '/products/nested/1', 'Nested 2' =&gt; '/products/nested/2' ) ), 'Company' =&gt; '/company', 'Privacy Policy' =&gt; '/privacy-policy' ); class NavBuilder extends RecursiveIteratorIterator { // stores the previous depth private $_depth = 0; // stores the current iteration's depth private $_curDepth = 0; // store the iterator protected $_it; /** * Constructor. * * @access public * @param Traversable $it * @param int $mode * @param int $flags */ public function __construct(Traversable $it, $mode = RecursiveIteratorIterator::SELF_FIRST, $flags = 0) { parent::__construct($it, $mode, $flags); // store the caching iterator $this-&gt;_it = $it; } /** * Override the return values. * * @access public */ public function current() { // the return output string $output = ''; // set the current depth $this-&gt;_curDepth = parent::getDepth(); // store the difference in depths $diff = abs($this-&gt;_curDepth - $this-&gt;_depth); // get the name and url of the nav item $name = parent::key(); $url = parent::current(); // close previous nested levels if ($this-&gt;_curDepth &lt; $this-&gt;_depth) { $output .= str_repeat('&lt;/ul&gt;&lt;/li&gt;', $diff); } // check if we have the last nav item if ($this-&gt;hasNext()) { $output .= '&lt;li&gt;&lt;a href="' . $url . '"&gt;' . $name . '&lt;/a&gt;'; } else { $output .= '&lt;li class="last"&gt;&lt;a href="' . $url . '"&gt;' . $name . '&lt;/a&gt;'; } // either add a subnav or close the list item if ($this-&gt;hasChildren()) { $output .= '&lt;ul&gt;'; } else { $output .= '&lt;/li&gt;'; } // cache the depth $this-&gt;_depth = $this-&gt;_curDepth; // return the output ( we could've also overridden current()) return $output; } } ?&gt; </code></pre> <p><strong>Usage</strong></p> <pre><code>&lt;?php try { // generate the recursive caching iterator $it = new RecursiveCachingIterator(new RecursiveArrayIterator($nav)); // build the navigation with the iterator $it = new NavBuilder($it, RecursiveIteratorIterator::SELF_FIRST); // display the resulting navigation echo '&lt;ul id="nav"&gt;' . PHP_EOL; foreach ($it as $value) { echo $value . "\n"; } echo '&lt;/ul&gt;' . PHP_EOL; } catch (Exception $e) { var_dump($e); die; } ?&gt; </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload