Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Ok, i have managed to cobble together a piece of code which seems to work.</p> <p>I am manually concancenating columns to /column1/column2/column3/column4/column5 strings in a one dimensional array first; Then applying the functions from url given in question to explode this simple array into an array tree. Finally i make use of the makeULLI function to generate an unordered list, with links for each node present. I have extended the code further to add custom paths to URLs depending how deep the link is, for use in SEO friendly links but i stripped that bit out.</p> <p>I am attaching the code which does all this and is suprisingly small. The code should work on any number of levels, I have run it on a table with around 400 rows (5 columns) and it executes in 0.0168 seconds. If anyone can see further optimisations to the code i would be grateful.</p> <pre><code> foreach ($geoArray as $result) { $mynewGEO[] = $result['parent']; $mynewGEO[] = $result['parent'].'/'.$result['child']; $mynewGEO[] = $result['parent'].'/'.$result['child'].'/'.$result['grandchild']; $mynewGEO[] = $result['parent'].'/'.$result['child'].'/'.$result['grandchild'].'/'.$result['grand-grandchild']; $mynewGEO[] = $result['parent'].'/'.$result['child'].'/'.$result['grandchild'].'/'.$result['grand-grandchild'].'/'.$result['grand-grand-grandchild']; } $key_files = array_combine(array_values($mynewGEO), array_values($mynewGEO)); $tree = explodeTree($key_files, "/", true); echo makeULLI($tree); function makeULLI($array) { $return = "&lt;ul&gt;\n"; if (is_array($array) &amp;&amp; count($array) &gt; 0) { foreach ($array as $k =&gt; $v) { if($k == "__base_val") continue; // determine the __base_val value in orde to use for the link. $our_linky = ( is_array($v) ? $v["__base_val"] : $v ); if (is_array($v) &amp;&amp; count($v) &gt; 0) { $return .= "\t&lt;li&gt;&lt;a href=\"".$our_linky."/\" &gt;" . $k ."&lt;/a&gt;". makeULLI($v) . "&lt;/li&gt;\n"; } else { $return .= "\t&lt;li&gt;&lt;a href=\"".$our_linky."/\" &gt;" . $k . "&lt;/a&gt;&lt;/li&gt;\n"; //to return full path //$return .= "\t&lt;li&gt;&lt;a href=\" # \" &gt;" . $v . "&lt;/a&gt;&lt;/li&gt;\n"; } } } else {} $return .= "&lt;/ul&gt;"; return $return; } function explodeTree($array, $delimiter = '_', $baseval = false) { if(!is_array($array)) return false; $splitRE = '/' . preg_quote($delimiter, '/') . '/'; $returnArr = array(); foreach ($array as $key =&gt; $val) { // Get parent parts and the current leaf $parts = preg_split($splitRE, $key, -1, PREG_SPLIT_NO_EMPTY); $leafPart = array_pop($parts); // Build parent structure // Might be slow for really deep and large structures $parentArr = &amp;$returnArr; foreach ($parts as $part) { if (!isset($parentArr[$part])) { $parentArr[$part] = array(); } elseif (!is_array($parentArr[$part])) { if ($baseval) { $parentArr[$part] = array('__base_val' =&gt; $parentArr[$part]); } else { $parentArr[$part] = array(); } } $parentArr = &amp;$parentArr[$part]; } // Add the final part to the structure if (empty($parentArr[$leafPart])) { $parentArr[$leafPart] = $val; } elseif ($baseval &amp;&amp; is_array($parentArr[$leafPart])) { $parentArr[$leafPart]['__base_val'] = $val; } } return $returnArr; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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