Category hierarchy from array(cat id => parent id)

后端 未结 6 1947
你的背包
你的背包 2021-01-24 23:26

i am trying to create a multidimensional array hierarchy from a simple array which contains pairs of category ids and parent ids. The categories can be a parent and a subcategor

6条回答
  •  无人及你
    2021-01-24 23:44

    Get your list :

     0,
      2 => 1,
      3 => 2,
      4 => 0,
      5 => 4,
      6 => 0
    );
    
    
    $menus = array();
    
    function generateMenus($start) {
        global $initialArray;
        foreach($initialArray as $k => $v) {
           if($start == $v) {
              $menus[] = $k;
           }
        }
        return $menus;
    }
    $final = array();
    foreach($initialArray as $key => $value) {
       $final[$value] = generateMenus($value);
    }
    
    echo '
      '; function generateList($start) { global $final; echo '
    • '.$start.'
    • '; if(!empty($final[$start])) { echo '
        '; foreach($final[$start] as $v) { generateList($v); } echo '
      '; } } generateList(0); echo '
    '; ?>

    It will produce

提交回复
热议问题