douqiaotong8682 2017-12-18 10:29
浏览 62

多维数组层次结构

I like to know how can I get the hierarchy by the identifier.

This is an example :

$inputArray = array(
        array(
            "text" => "Dir1",
            "parent_id" => "",
            "id" => "1",
            "filesize" => "109"
    ),array(
        "text" => "dir2",
        "parent_id" => "",
        "id" => "2",
        "filesize" => "88",
        "children" => array(
                "text" => "Dir3",
                "parent_id" => "2",
                "id" => "3",
                "filesize" => "",
                "children" => array(
                    "text" => "dir4",
                    "parent_id" => "3",
                    "id" => "4",
                    "filesize" => "",
                    "children" => array(
                        "text" => "dir5",
                        "parent_id" => "4",
                        "id" => "4",
                        "filesize" => ""
                    )
                )
        )
    ));

looking for this example :

dir3/dir4/dir5
  • 写回答

1条回答 默认 最新

  • douzong6649 2017-12-18 10:58
    关注
    function getText($array) {
      $save[]  = $array['text'];
      if (isset($array['children'])) {
         $save = array_merge($save, getText($array['children']));
      }
      return $save;  
    }
    
    foreach($inputArray as $x) {
         echo implode('/', getText($x)) . "
    ";
    }
    

    result

    Dir1
    dir2/Dir3/dir4/dir5
    

    demo

    评论

报告相同问题?

悬赏问题

  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作