Skip to content

Instantly share code, notes, and snippets.

@vyspiansky
Last active September 9, 2024 14:33
Show Gist options
  • Save vyspiansky/6552875 to your computer and use it in GitHub Desktop.
Save vyspiansky/6552875 to your computer and use it in GitHub Desktop.
PHP: convert object / array to tree structure
<?php
/**
* The below functions take the single level array of items as an argument
* and turn it into a multidimensional array structure (tree),
* where each item has an array of sub-items.
*
* Each item should have at least an `id` and `parent_id`,
* where the `parent_id` is `0` if it's top level.
*
* Source: http://goo.gl/p2GybZ
*/
// for object
function buildTreeFromObjects($items) {
$childs = [];
foreach ($items as $item)
$childs[$item->parent_id][] = $item;
foreach ($items as $item) if (isset($childs[$item->id]))
$item->childs = $childs[$item->id];
return $childs[0];
}
// or array version
function buildTreeFromArray($items) {
$childs = [];
foreach ($items as &$item) $childs[$item['parent_id']][] = &$item;
unset($item);
foreach ($items as &$item) if (isset($childs[$item['id']]))
$item['childs'] = $childs[$item['id']];
return $childs[0];
}
// usage:
$arrData = [
// Business
['id' => 1, 'name' => 'Business', 'parent_id' => 0],
['id' => 5, 'name' => 'Books and magazines', 'parent_id' => 1],
['id' => 6, 'name' => 'Electronics and telecom', 'parent_id' => 1],
// Computers
['id' => 2, 'name' => 'Computers', 'parent_id' => 0],
// Health
['id' => 3, 'name' => 'Health', 'parent_id' => 0],
['id' => 7, 'name' => 'Addictions', 'parent_id' => 3],
['id' => 8, 'name' => 'Dentistry', 'parent_id' => 3],
['id' => 9, 'name' => 'Vision Care', 'parent_id' => 3],
// Sports
['id' => 4, 'name' => 'Sports', 'parent_id' => 0],
['id' => 10, 'name' => 'Winter Sports', 'parent_id' => 4],
['id' => 11, 'name' => 'Ice skating', 'parent_id' => 10],
['id' => 12, 'name' => 'Sledding', 'parent_id' => 10],
];
$tree = buildTreeFromArray($arrData);
@woteska
Copy link

woteska commented Aug 30, 2018

Thanks buddy! ;) Was very helpful.

@voidnerd
Copy link

this is great, you came through for me @vyspiansky

@szambonurek
Copy link

Updated version:

function buildTreeFromArray($items)
{
    $childs = [];

    foreach ($items as &$item) {
        $childs[$item['parent_id'] ?? 0][] = &$item;
    }

    unset($item);

    foreach ($items as &$item) {
        if (isset($childs[$item['id']])) {
            $item['childs'] = $childs[$item['id']];
        }
    }

    return $childs[0] ?? [];
}

function buildTreeFromObjects($items)
{
    $childs = [];

    foreach ($items as $item) {
        $childs[$item->parent_id ?? 0][] = $item;
    }

    foreach ($items as $item) {
        if (isset($childs[$item->id])) {
            $item->childs = $childs[$item->id];
        }
    }

    return $childs[0] ?? [];
}

@vyspiansky
Copy link
Author

vyspiansky commented Jul 16, 2019

@szambonurek Looks good. Thanks!

@zim32
Copy link

zim32 commented Oct 24, 2019

Not working at all

@daler2204
Copy link

daler2204 commented Aug 18, 2020

@szambonurek its working, Thanks!!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment