简体   繁体   中英

What is the most efficient way to change a list to a nested object in php

I have a list of (the result of a query in DB) like this:

[
    {
        "id": "1",
        "parent_id": null,
        "title": "مدادنوکی",
        "url": "/medadnoki"
    },
    {
        "id": "2",
        "parent_id": null,
        "title": "جامعه",
        "url": "/commiunity"
    },
    {
        "id": "5",
        "parent_id": "1",
        "title": "درباره ی مدادنوکی",
        "url": "/about"
    },
    {
        "id": "6",
        "parent_id": "1",
        "title": "درباره ی مدادنوکی",
        "url": "/about"
    },
    {
        "id": "7",
        "parent_id": "2",
        "title": "همکاران",
        "url": "/co-worker"
    },
    {
        "id": "8",
        "parent_id": "2",
        "title": "اساتید",
        "url": "/masters"
    }
]

But I want to create an object like this:

[
                {
                    "title": "مدادنوکی",
                    "url": "/medadnoki",
                    "subs" : [
                        {
                            "title": "درباره مدادنوکی",
                            "url": "/about"
                        },
                        {
                            "title": "درباره مدادنوکی",
                            "url": "/about"
                        },
                        {
                            "title": "درباره مدادنوکی",
                            "url": "/about"
                        },
                        {
                            "title": "درباره مدادنوکی",
                            "url": "/about"
                        }
                    ]
                },
                {
                    "title": "جامعه",
                    "url": "/soc",
                    "subs" : [
                        {
                            "title": "همکاران",
                            "url": "/co-work"
                        },
                        {
                            "title": "اساتید",
                            "url": "/masters"
                        }
                    ]
                }
            ]

I have handled this process with two foreach in php and it means I process data twice and it is not efficient and will be slow. Is there any Idea to doing this with just one foreach ? using one foreach means faster than two foreach twice and It will show the result in big data

Assuming you have the results in an order where the parents appear in the results before any children they may have, this should work:

$nested = [];
foreach($results as $r) {
  if($r['parent_id'] === null) {
    $nested[$r['id']] = [
      'title' => $r['title'],
      'url'   => $r['url'],
      'subs'  => []
    ];
    continue;
  }
  $nested[$r['parent_id']]['subs'][] = [
    'title' => $r['title'],
    'url'   => $r['url']
  ];
}
$nested = array_values($nested);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM