Note that there are some explanatory texts on larger screens.

plurals
  1. POphp data structure - xml or tree?
    text
    copied!<p>i want to write a generalized function that can output something like this. </p> <pre><code>&lt;span class="side_title"&gt;By Collection&lt;/span&gt; &lt;ul class="menu"&gt; &lt;li class="top"&gt; &lt;a class="top_menu" href="#url"&gt;home&lt;/a&gt; &lt;/li&gt; &lt;li class="sub"&gt; &lt;a href="#"&gt;collectionA&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="../index.html"&gt;collectionA-1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#url"&gt;collectionA-2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="sub"&gt; &lt;a href="#"&gt;collectionB&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#url"&gt;collectionB-1&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="top"&gt;&lt;a href="#url"&gt;CollectionE&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;span class="side_title"&gt;By Functions&lt;/span&gt; &lt;ul class="menu"&gt; &lt;li class="top"&gt; &lt;a class="top_menu" href="#url"&gt;home&lt;/a&gt; &lt;/li&gt; &lt;li class="sub"&gt; &lt;a href="#"&gt;functionA&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="../index.html"&gt;functionA-1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#url"&gt;functionA-2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="sub"&gt; &lt;a href="#"&gt;functionB&lt;/a&gt; &lt;ul&gt; &lt;li&gt;&lt;a href="#url"&gt;functionB-1&lt;/a&gt;&lt;/li&gt; &lt;li&gt;&lt;a href="#url"&gt;functionB-2&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; &lt;/li&gt; &lt;li class="top"&gt;&lt;a href="#url"&gt;functionE&lt;/a&gt;&lt;/li&gt; &lt;/ul&gt; </code></pre> <p>but the problem is i can't find a data structure to represent this structure, i tried the follwoing(using array - where the key is link's content, value is the address. and recursion): </p> <pre><code>$a = array ( "By collection" =&gt; '', 'Base' =&gt; 'thisBase', "expand" =&gt; array ( 'Home' =&gt; 'home', 'collectionA' =&gt; 'collectionA', 'expand' =&gt; array ( 'collectiona-1' =&gt; 'collectiona-1', 'collectiona-2' =&gt; 'collectiona-2' ), 'collectionB' =&gt; 'collectionb', 'expand' =&gt; array ( 'collectionb-1' =&gt; 'collectionb-1', 'collectionb-2' =&gt; 'collectionb-2' ), 'collectionB' =&gt; 'collectionb', 'expand' =&gt; array ( 'collectionc-1' =&gt; 'collectionc-1', 'collectionc-2' =&gt; 'collectionc-2' ), ), "by Function" =&gt; '' ); function expand($a=NULL) { foreach ($a as $key =&gt; $value) { if ($value == '') { echo '&lt;span class="side_title"&gt;'.$key.'&lt;/span&gt;'; echo '&lt;ul class="menu"&gt;'; } elseif (is_array($value)) { echo '&lt;ul class="sub_menu"&gt;'; expand($value); } else { echo '&lt;li&gt;&lt;a href="'.$value.'&gt;'.$key.'&lt;/a&gt;&lt;/li&gt;'; } } echo '&lt;/ul&gt;'; } print_r($a); expand($a); </code></pre> <p>i thought about using xml/tree to represent it, but the data structure is pass between different function, so i thought it will be to much effort, so i want to know what's wrong with this? or is there better way?</p>
 

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