0

So, basically what i'm trying to do is getting combinations of the array children, for example i got the array:

[
"name" => "Item1", 
"children" => 
        [
        "name" => "Item2",
        "children" => [
                ["name" => "Item3"],
                ["name" => "Item4"]
                ]
        ],
        ["name" => "Item5"]
];

I tried to work with some functions i got on the stackoverflow, but i only got it to work with all of them at once, i was getting just

[
"Item4" => "Item1/Item2/Item4",
"Item5" => "Item1/Item5"
];

The output should be

[
"Item1" => "Item1",
"Item2" => "Item1/Item2",
"Item3" => "Item1/Item2/Item3"
"Item4" => "Item1/Item2/Item4"
"Item5" => "Item1/Item5"
];

As asked, the function i was working with before:

function flatten($arr) {
    $lst = [];
    /* Iterate over each item at the current level */
    foreach ($arr as $key => $item) {
        /* Get the "prefix" of the URL */
        $prefix = $item['slug'];
        /* Check if it has children */
        if (array_key_exists('children', $item) and sizeof($item['children'])) {
            /* Get the suffixes recursively */
            $suffixes = flatten($item['children']);
            /* Add it to the current prefix */
            foreach($suffixes as $suffix) {
                $url = $prefix . '/' . $suffix;
                $lst[$item['id']] = $url;
            }
        } else {
            /* If there are no children, just add the
             * current prefix to the list */
            $lst[$item['id']] = $prefix;
        }
    }

    return $lst;
}
0

1 Answer 1

1

I've had to fix the data as the levels of data don't match up. The rest of the code is new as I found so many errors from your existing code.

Comments in code...

$data = [
    "name" => "Item1",
    "children" =>
    [[
        "name" => "Item2",
        "children" =>[
        ["name" => "Item3"],
        ["name" => "Item4"]]
    ],
    ["name" => "Item5"]]
];

print_r(flatten($data));

function flatten($arr, $pathSoFar = '') {
    $lst = [];
    $path = $pathSoFar."/";
    foreach ( $arr as $key => $value )  {
        if ( $key === 'name' )   {
            // Add name of current level onto path
            $path .= $value;
            $lst[$value] = $path;
        }
        else if ( $key === 'children' )  {
            //Process child elements recursively and add into current array
            $lst = array_merge($lst, flatten($value, $path));
        }
        else    {
            // This is for sub-elements which probably are (for example) 0, 1
            // (removing trailing / to stop multiples)
            $lst = array_merge($lst, flatten($value, rtrim($path,"/")));
        }
    }
    return $lst;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, i got it working with your code, but i still dont understand why it returns each possibility instead of the sum of them
You can either step through the code or add in debugging data to show what it is working on.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.