php - How To Get Data from JSON Into Unordered List? -


i have json file contain this:

"menus": [{     "menuid": "1",     "menuname": "perencanaan dan pengadaan",     "menuparent": "",     "menulink": "" }, {     "menuid": "1-1",     "menuname": "rka / dpa",     "menuparent": "1",     "menulink": "" }, {     "menuid": "1-1-1",     "menuname": "daftar rka / dpa",     "menuparent": "1-1",     "menulink": "rkbu" }, 

i want put data unordered list dynamically. output want (with 3 level list):

perencanaan dan pengadaan     rka / dpa         daftar rka / dpa 

i have tried code:

echo "<ul>";  foreach($get_data['menus'] $node){     if(strlen($node['menuid']) == 1){         echo "<li>" . $node['menuname'];         echo "</li>";     }         echo "<ul>";             if(strlen($node['menuid']) == 3){                 echo "<li>".$node['menuname']."</li>";         }                 if(strlen($node['menuid']) == 5){                     echo "<ul>";                         echo "<li>".$node['menuname']."</li>";                     echo "</ul>";                 }     echo "</ul>"; }  echo "</ul>"; 

but find not dynamic because depends on string length. i've read best method using recursive method. cannot find recursive pattern of json file. can me find solution? thanks

i don't think possible make recursive calls directly on flat json data.

i suggest first convert flat data multidimensional array , afterwards recursively generate menu.

i took parts of code here: dynamically creating/inserting associative array in php

$get_data = array(     array(         "menuid" => "1",         "menuname" => "perencanaan dan pengadaan",         "menuparent" => "",         "menulink" => ""     ),     array(         "menuid" => "1-1",         "menuname" => "rka / dpa",         "menuparent" => "1",         "menulink" => ""     ),     array(         "menuid" => "1-1-1",         "menuname" => "daftar rka / dpa",         "menuparent" => "1-1",         "menulink" => "rkbu"     ) );  function insert_into(&$array, array $keys, $value) {      $last = array_pop($keys);              foreach($keys $key) {           if(!array_key_exists($key, $array) ||                array_key_exists($key, $array) && !is_array($array[$key])) {                   $array[$key]['items'] = array();            }           $array = &$array[$key]['items'];      }      $array[$last]['value'] = $value; }  function create_menu($menuitems) {     $content = '<ul>';     foreach($menuitems $item) {         $content .= '<li>' . $item['value'];         if(isset($item['items']) && count($item['items'])) {             $content .= create_menu($item['items']);         }         $content .=  '</li>';     }     $content .= '</ul>';     return $content; }  $menuitems = array(); foreach($get_data $item) {     $levels = explode('-', $item['menuid']);     insert_into($menuitems, $levels, $item['menuname']); } print_r($menuitems); print create_menu($menuitems); 

demo: http://3v4l.org/drk4f

output:

array (      [1] => array (          [value] => perencanaan dan pengadaan          [items] => array (              [1] => array (                  [value] => rka / dpa                  [items] => array (                      [1] => array (                          [value] => daftar rka / dpa                      )                  )              )          )      )  )  <ul>     <li>perencanaan dan pengadaan         <ul>             <li>rka / dpa                 <ul>                     <li>daftar rka / dpa</li>                 </ul>             </li>         </ul>     </li> </ul> 

Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -