Flat/Merge Array of Arrays and primitve types

i have an Array like this:

[ [ "id": 1, "name": "Kategorie 1", "parent_id": null, [ "id": 2, "name": "Kategorie 1.1", "parent_id": 1, [ "id": 5, "name": "Kategorie 1.1.1", "parent_id": 2, [ "id": 11, "name": "Kategorie 1.1.1.1", "parent_id": 5,  [] ], [ "id": 12, "name": "Kategorie 1.1.1.2", "parent_id": 5, [] ] , ........]

So this is a Category Hierachy like:
Category 1
 Category 1.1
 Category 1.2
  Category 1.2.1
   Category 1.2.1.1.
Category 2
 Category 2.1.
  Category 2.1.1.
 Category 2.2
 Category 2.3.

But the depth of the Categories is variable.

So is there a way to flat/merge the Array to:

[ [ "id": 1, "name": "Kategorie 1", "parent_id": null], [ "id": 2, "name": "Kategorie 1.1", "parent_id": 1],  [ "id": 5, "name": "Kategorie 1.1.1", "parent_id": 2], [ "id": 11, "name": "Kategorie 1.1.1.1", "parent_id": 5], [ "id": 12, "name": "Kategorie 1.1.1.2", "parent_id": 5] ]

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

There you go:

<?php
$array = [
    [
        "id" => 1, "name" => "Kategorie 1", "parent_id" => null,
        [
            "id" => 2, "name" => "Kategorie 1.1", "parent_id" => 1,
            [
                "id" => 5, "name" => "Kategorie 1.1.1", "parent_id" => 2,
                [
                    "id" => 11, "name" => "Kategorie 1.1.1.1", "parent_id" => 5, []
                ],
                [
                    "id" => 12, "name" => "Kategorie 1.1.1.2", "parent_id" => 5, []
                ]
            ]
        ]
    ]
];

function flatMerge(array $array, array &$result = [])
{
    $subResult = [];
    foreach ($array as $key => $sub) {
        if (is_array($sub)) {
            flatMerge($sub, $result);
        } else {
            $subResult[$key] = $sub;
        }
    }
    $result[] = $subResult;
    return $result;
}

$result = flatMerge($array); // flat merge the array
var_dump($result);
$result = array_filter($result, static function (array $array) { // remove empty arrays
    return !empty($array);
});
var_dump($result);
$result = array_values($result); // reset array keys
var_dump($result);

You can see the result of this code here: http://sandbox.onlinephpfunctions.com/code/b4eaddb4a52f5e9cf9a80644190e526613381c60

Method 2

Supposing your array like this:

$arr = [ 
  [ 
    "id" => 1, "name" => "Kategorie 1", "parent_id" => null, 
    "children" => [
      "id" => 2, "name" => "Kategorie 1.1", "parent_id" => 1, 
      "children" => [ 
        "id" => 5, "name" => "Kategorie 1.1.1", "parent_id" => 2, 
        children => [
          [ "id" => 11, "name" => "Kategorie 1.1.1.1", "parent_id" => 5, "children" => [] ], 
          [ "id" => 12, "name" => "Kategorie 1.1.1.2", "parent_id"=> 5, "children" => [] ]
        ]
      ]
    ]
  ]
];

The following function may work in your case, using recursion function:
function flatten(array $array) {
    $branch = [];
    foreach ($array as $item) {
        $children = [];
        if (isset($item["children"]) && is_array($item["children"])) {
            $children = flatten($item["children"]);
            unset($item["children"]);
        }
        $branch = array_merge($branch, [$item], $children);
    }

    return $branch;
}


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x