[PHP] Nested array, multidimensional

Hello guys.

Got an array

Array
(
    [cats] => Array
        (
            [1] => Array
                (
                    [id] => 1
                    [name] => women
                    [pID] => 0
                )

            [2] => Array
                (
                    [id] => 2
                    [name] => fashion
                    [pID] => 1
                )

            [3] => Array
                (
                    [id] => 3
                    [name] => sport
                    [pID] => 2
                )

            [4] => Array
                (
                    [id] => 4
                    [name] => home
                    [pID] => 1
                )

            [5] => Array
                (
                    [id] => 5
                    [name] => men
                    [pID] => 4
                )

        )

)

using

$arr = array();
   foreach($catsL['cats'] as $k => $c) {
     if(($c['pID']) > 0) {
         $array[$c['pID']]['children'][] = $c;
     } else {
         $array[$c['id']] = $c;
     }
}

Want to get array like:

item/id/parent

item-1 / 2 / 0
item-1 / 3 / 2 - child of element with id 2
item-1 / 4 / 3 - child of element with id 3

Got:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => women
            [pID] => 0
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 2
                            [name] => fashion
                            [pID] => 1
                        )

                    [1] => Array
                        (
                            [id] => 4
                            [name] => home
                            [pID] => 1
                        )

                )

        )

    [1] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 3
                            [name] => sport
                            [pID] => 2
                        )

                )

        )

    [2] => Array
        (
            [children] => Array
                (
                    [0] => Array
                        (
                            [id] => 5
                            [name] => men
                            [pID] => 4
                        )

                )

        )

)

Childs getting inserted as new elements of 1st level array not as child.

Any clues?

You are making the solving of this issue a bit harder for us than is necessary.

Instead of showing the result of (I think)


var_dump($catsL);

Show the result of:


echo var_export($catsL, 1);

Then instead of showing the expected result using a completely different nomenclature (“item1”)


item-1 / 2 / 0
item-1 / 3 / 2 - child of element with id 2
item-1 / 4 / 3 - child of element with id 3

Show us EXACTLY what you want - in the manner of the var_export() result, ie the exact makeup of the expected computed array.

There are many who can do this kind of modelling in their heads, but I am not one of them, I need a test case I can copy/paste and concrete result to which I can aim – then I will have a go.

I don’t think I am alone though, as your Q has gone unanswered in ~16hrs :wink:

Your code, the array you begin with, and the array you end up with just plain doesnt make sense.

[1] => Array
                (
                    [id] => 1
                    [name] => women
                    [pID] => 0
                ) 

Here, pID is 0.

Which means your else-block should run…

  $array[$c['id']] = $c; 

Yet, your resulting array is not using

$c['id']

as its key here:

[0] => Array
        (
            [id] => 1
            [name] => women
            [pID] => 0
            [children] => Array
                ( 

It’s using zero?
What truly baffles me… The two arrays in children manages to plop into the “right” place, while the key is wrong!
Bafflement, somethings wrong, and i cant see what.