0

i have array like this:

(
    [0] => Array
        (
            [id] => 1
            [name] => Bazowa
            [parent_id] => 0
        )

    [1] => Array
        (
            [id] => 2
            [name] => Główna
            [parent_id] => 1
        )

    [2] => Array
        (
            [id] => 12
            [name] => PlayStation
            [parent_id] => 2
        )

    [3] => Array
        (
            [id] => 13
            [name] => Xbox
            [parent_id] => 2
        )

    [4] => Array
        (
            [id] => 14
            [name] => Nintendo
            [parent_id] => 2
        )

    [5] => Array
        (
            [id] => 15
            [name] => PC
            [parent_id] => 2
        )
)

and i want sort this array like tree on screenshot below: Screen of tree what I want

i trying use this Sort array values based on parent/child relationship

        foreach($xml->children()->children() as $value) {
        if($value->active == 1) {
            $categories[] = [
                'id' => (int) $value->id,
                'name' => (string) $value->name->language,
                'parent_id' => (int) $value->id_parent
            ];
        }
    }
    $parent_ids = [];

    $parents = ['' => []];
    foreach($categories as $val) {
        $parents[$val['parent_id']][] = $val;
    }
    $sorted = $parents[''];
    dump($parents); exit;
    for($val = reset($sorted); $val; $val = next($sorted)) {
        if(isset($parents[$val[0]])) {
            foreach($parents[$val[0]] as $next) {
                $sorted[] = $next;
            }
        }
    }

The most important thing for me is that everything displays well in select, which is something like this:

-Playstation
-- Playstation 5
--- Gry
--Playstation 3
--- Gry
Anyone can help me?

EDIT: Problem solved by Build a tree from a flat array in PHP

Blaaszka
  • 1
  • 1
  • What did you try? The simplest solution is creating a category tree in form of multilevel array. for sorting by value of subarray you should use [usort()](https://www.php.net/manual/en/function.usort.php), however using "I want to" + screenshot doesn't describe your problem good enough. Also even if using screenshot include it here and display it inline instead of linking to the third part service. – biesior May 22 '21 at 16:58
  • @biesior ok, sorry, I corrected the post, hope you can understand it better now – Blaaszka May 22 '21 at 17:08
  • You have relations here between parent-child elements, so you need to create a nested array representing the tree, you can do it using PHP or JS, depends on your needs. If I understood properly, while it works in another part of the code (for the select) you can check how it's done there and copy the solution. – biesior May 22 '21 at 17:17
  • I don't quite understand how I could do this – Blaaszka May 22 '21 at 18:52
  • creating trees is much easier using recursive to fetch data from data base, can you access it like that or you have only this array? – biesior May 22 '21 at 19:36

1 Answers1

1

This should work

usort($arr, function($a, $b) {
 return $a["parent_id"] > $b["parent_id"];
});
  • It doesn't work the way it wants to. This way I only sort by parent ID, and I want it to go in the appropriate tree that I indicated in the example. – Blaaszka May 22 '21 at 18:51