Skip to content
Advertisement

Organize array based on hierarchy of parent field

I have a table for ‘Categories’ on the application I am building with PHP. Each category can have a children category and so on. For example:

-Computer
--Laptops
---Apple
---Dell
--Monitor
---LCD
---Ultrawide

I need to organize an array hierarchically like the example above. Considering that on my Table I only save 3 fields:

id
name_cateogry
parent_id

So I save only the first direct parent_id. If it is the first level, parent_id is null. For all other levels, parent_id receives the id of the parent level.
I am having a hard time creating a script that organizes the array hierarchically, considering many levels.
I would like a suggestion of how to accomplish that with PHP, so I can show the categories hierarchically on the application.

Advertisement

Answer

This function will help you, you can make an tree from your data.
So first you need to fetch all the data and then use this makeTree function to make a tree for it

<?php

$array = [
    [    
        "id" => 1,
        "name" => "hi",
        "parent_id" => null,
    ],
    [    
        "id" => 2,
        "name" => "hi2",
        "parent_id" => 1,
    ],
    [    
        "id" => 3,
        "name" => "hi3",
        "parent_id" => 2,
    ],
    [    
        "id" => 4,
        "name" => "h4",
        "parent_id" => 1,
    ],
    [    
        "id" => 5,
        "name" => "hi5",
        "parent_id" => null,
    ],
];

function makeTree(array $elements, $parentId = 0) {
    $branch = array();
    foreach ($elements as $element) {
        if ($element['parent_id'] == $parentId) {
            $children = makeTree($elements, $element['id']);
            if ($children) {
                $element['children'] = $children;
            }
            $branch[] = $element;
        }
    }
    return $branch;
}

$tree = makeTree($array);
print_r( $tree );

output

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => hi
            [parent_id] => 
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => hi2
                            [parent_id] => 1
                            [children] => Array
                                (
                                    [0] => Array
                                        (
                                            [id] => 3
                                            [name] => hi3
                                            [parent_id] => 2
                                        )
                                )
                        )
                    [1] => Array
                        (
                            [id] => 4
                            [name] => h4
                            [parent_id] => 1
                        )
                )
        )
    [1] => Array
        (
            [id] => 5
            [name] => hi5
            [parent_id] => 
        )
)
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement