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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?