Home > Blockchain >  Convert array of object to nested array like recursive menu, not echo html
Convert array of object to nested array like recursive menu, not echo html

Time:07-24

I have a input like this, maybe data has more and more:

[
    [
        "id" => "uuid-1",
        "createdAt" => "2021-02-26T00:55:36.632Z",
        "name" => "Webprovise Corp",
        "parentId" => "0"
    ],
    [
        "id" => "uuid-2",
        "createdAt" => "2021-02-25T10:35:32.978Z",
        "name" => "Stamm LLC",
        "parentId" => "uuid-1"
    ],
    [
        "id" => "uuid-3",
        "createdAt" => "2021-02-25T15:16:30.887Z",
        "name" => "Blanda, Langosh and Barton",
        "parentId" => "uuid-1"
    ],
    [
        "id" => "uuid-4",
        "createdAt" => "2021-02-25T06:11:47.519Z",
        "name" => "Price and Sons",
        "parentId" => "uuid-2"
    ],
    [
        "id" => "uuid-5",
        "createdAt" => "2021-02-25T13:35:57.923Z",
        "name" => "Hane - Windler",
        "parentId" => "uuid-3"
    ],
    [
        "id" => "uuid-6",
        "createdAt" => "2021-02-26T01:41:06.479Z",
        "name" => "Vandervort - Bechtelar",
        "parentId" => "uuid-3"
    ],
    [
        "id" => "uuid-7",
        "createdAt" => "2021-02-25T07:56:32.335Z",
        "name" => "Zieme - Mills",
        "parentId" => "uuid-2"
    ]
]

And output I want is, only using php to make it:

[
    "uuid-1" => [
        "id" => "uuid-1",
        "createdAt" => "2021-02-26T00:55:36.632Z",
        "name" => "Webprovise Corp",
        "childrens" => [
            [
                "id" => "uuid-2",
                "createdAt" => "2021-02-25T10:35:32.978Z",
                "name" => "Stamm LLC",
                "parentId" => "uuid-1",
                "childrens" => [
                    [
                        "id" => "uuid-4",
                        "createdAt" => "2021-02-25T06:11:47.519Z",
                        "name" => "Price and Sons",
                        "parentId" => "uuid-2"
                    ],
                    [
                        "id" => "uuid-7",
                        "createdAt" => "2021-02-25T07:56:32.335Z",
                        "name" => "Zieme - Mills",
                        "parentId" => "uuid-2"
                    ]
                ]
            ],
            [
                "id" => "uuid-3",
                "createdAt" => "2021-02-25T15:16:30.887Z",
                "name" => "Blanda, Langosh and Barton",
                "parentId" => "uuid-1",
                "childrens" => [
                    [
                        "id" => "uuid-5",
                        "createdAt" => "2021-02-25T13:35:57.923Z",
                        "name" => "Hane - Windler",
                        "parentId" => "uuid-3"
                    ],
                    [
                        "id" => "uuid-6",
                        "createdAt" => "2021-02-26T01:41:06.479Z",
                        "name" => "Vandervort - Bechtelar",
                        "parentId" => "uuid-3"
                    ]
                ]
            ],
        ]
    ]   
]

I can easily show this menu tree in web browser using javascript but with only php I have no idea. How do I do this? And maybe it has many many deep childrens level. Please give me an idea.

CodePudding user response:

Hope so this will help you

<?php
$arr=[
    [
        "id" => "uuid-1",
        "createdAt" => "2021-02-26T00:55:36.632Z",
        "name" => "Webprovise Corp",
        "parentId" => "0"
    ],
    [
        "id" => "uuid-2",
        "createdAt" => "2021-02-25T10:35:32.978Z",
        "name" => "Stamm LLC",
        "parentId" => "uuid-1"
    ],
    [
        "id" => "uuid-3",
        "createdAt" => "2021-02-25T15:16:30.887Z",
        "name" => "Blanda, Langosh and Barton",
        "parentId" => "uuid-1"
    ],
    [
        "id" => "uuid-4",
        "createdAt" => "2021-02-25T06:11:47.519Z",
        "name" => "Price and Sons",
        "parentId" => "uuid-2"
    ],
    [
        "id" => "uuid-5",
        "createdAt" => "2021-02-25T13:35:57.923Z",
        "name" => "Hane - Windler",
        "parentId" => "uuid-3"
    ],
    [
        "id" => "uuid-6",
        "createdAt" => "2021-02-26T01:41:06.479Z",
        "name" => "Vandervort - Bechtelar",
        "parentId" => "uuid-3"
    ],
    [
        "id" => "uuid-7",
        "createdAt" => "2021-02-25T07:56:32.335Z",
        "name" => "Zieme - Mills",
        "parentId" => "uuid-2"
    ],
    [
        "id" => "uuid-8",
        "createdAt" => "2021-02-26T00:55:36.632Z",
        "name" => "Webprovise Corp",
        "parentId" => "0"
    ],
];

function findChild($uuid){
    global $arr;
    return array_filter($arr,function($value) use($uuid){
        return $value['parentId']==$uuid;
    });
}

function getChilds($childrens){
    $newarr=[];
    foreach ($childrens as $child) {
        $childs=findChild($child['id']);
        count($childs)?$child['childrens']=getChilds($childs):"";
        $newarr[$child['id']]=$child;
    }
    return $newarr;
}

function getParents(){
    global $arr;
    return array_filter($arr,function($value){
        return $value['parentId']=="0";
    });
}

function newArray(){
    $newarr=[];
    foreach (getParents() as $parent) {
        $childs=findChild($parent['id']);
        count($childs)?$parent['childrens']=getChilds($childs):"";
        $newarr[$parent['id']]=$parent;
    }
    return $newarr;
}

print_r(newArray());

Output:

{
   "uuid-1":{
      "id":"uuid-1",
      "createdAt":"2021-02-26T00:55:36.632Z",
      "name":"Webprovise Corp",
      "parentId":"0",
      "childrens":{
         "uuid-2":{
            "id":"uuid-2",
            "createdAt":"2021-02-25T10:35:32.978Z",
            "name":"Stamm LLC",
            "parentId":"uuid-1",
            "childrens":{
               "uuid-4":{
                  "id":"uuid-4",
                  "createdAt":"2021-02-25T06:11:47.519Z",
                  "name":"Price and Sons",
                  "parentId":"uuid-2"
               },
               "uuid-7":{
                  "id":"uuid-7",
                  "createdAt":"2021-02-25T07:56:32.335Z",
                  "name":"Zieme - Mills",
                  "parentId":"uuid-2"
               }
            }
         },
         "uuid-3":{
            "id":"uuid-3",
            "createdAt":"2021-02-25T15:16:30.887Z",
            "name":"Blanda, Langosh and Barton",
            "parentId":"uuid-1",
            "childrens":{
               "uuid-5":{
                  "id":"uuid-5",
                  "createdAt":"2021-02-25T13:35:57.923Z",
                  "name":"Hane - Windler",
                  "parentId":"uuid-3"
               },
               "uuid-6":{
                  "id":"uuid-6",
                  "createdAt":"2021-02-26T01:41:06.479Z",
                  "name":"Vandervort - Bechtelar",
                  "parentId":"uuid-3"
               }
            }
         }
      }
   },
   "uuid-8":{
      "id":"uuid-8",
      "createdAt":"2021-02-26T00:55:36.632Z",
      "name":"Webprovise Corp",
      "parentId":"0"
   }
}
  • Related