dongpankao6133 2017-05-29 17:24
浏览 41
已采纳

如何将关联数组转换为指定的格式?

I have outputted the multidimensional array of directories where dimensions in depth are the subfolders.

Output

array(4) {
  ["tt"]=>
  array(1) {
    ["nn"]=>
    array(1) {
      ["fff"]=>
      array(0) {
      }
    }
  }
  ["testgg"]=>
  array(3) {
    ["fff"]=>
    array(0) {
    }
    ["ggg"]=>
    array(2) {
      ["ttttt"]=>
      array(0) {
      }
      ["kk"]=>
      array(0) {
      }
    }
    ["bb"]=>
    array(1) {
      ["ssssss"]=>
      array(0) {
      }
    }
  }
  ["test"]=>
  array(3) {
    ["dd"]=>
    array(0) {
    }
    ["ew"]=>
    array(1) {
      ["re"]=>
      array(1) {
        ["ffff"]=>
        array(0) {
        }
      }
    }
    ["hh"]=>
    array(0) {
    }
  }
  ["eeeee"]=>
  array(2) {
    ["fff"]=>
    array(1) {
      ["test"]=>
      array(2) {
        [0]=>
        string(30) "Save-Image-File-Formats-2a.png"
        ["nnn"]=>
        array(1) {
          ["bbb"]=>
          array(0) {
          }
        }
      }
    }
    ["sss"]=>
    array(0) {
    }
  }
}

I need to make all the keys into a string in the format below

Format

{
    text: "Parent 1",
    nodes: [
        {
            text: "Child 1",
            nodes: [
                {
                    text: "Grandchild 1"
                },
                {
                    text: "Grandchild 2"
                }
            ]
        },
        {
            text: "Child 2",
            test: "hell yea"
        }
    ]
},
{
    text: "Parent 2"
},
{
    text: "Parent 3"
},
{
    text: "Parent 4"
},
{
    text: "Parent 5"
}

Where the child nodes represent the subfolders.

How can I implement a recursive function to convert the array into a string with the above format?

  • 写回答

1条回答 默认 最新

  • duanmei9980 2017-05-29 18:33
    关注
    <?php
    function tree($data) {
        $result = [];
        foreach ($data as $key => $value) {
            if (!is_array($value)) {
                $result[$key] = $value;
                continue;
            }
            if (count($value) === 0) {
                $result[] = ['text' => $key];
            } else {
                $result[] = ['text' => $key, 'nodes' => tree($value)];
            }
        }
    
        return $result;
    }
    
    $data = [
        'Parent 1' => [
            'Child 1' => [
                'Grandchild 1' => [],
                'Grandchild 2' => []
            ],
            'Child 2' => [],
        ],
        'Parent 2' => []
    ];
    
    echo json_encode(tree($data), JSON_PRETTY_PRINT);
    

    Result:

    [
        {
            "text": "Parent 1",
            "nodes": [
                {
                    "text": "Child 1",
                    "nodes": [
                        {
                            "text": "Grandchild 1"
                        },
                        {
                            "text": "Grandchild 2"
                        }
                    ]
                },
                {
                    "text": "Child 2"
                }
            ]
        },
        {
            "text": "Parent 2"
        }
    ]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图