I am trying to take a flat array and recreate it so that it’s multidimensional. I’ve been looking into array_combine and array_merge, but I’m not sure that either of those will give me what I’m hoping for…
The array, in it’s current form (and this is just a simplified example):
Array
(
[0] => stdClass Object
(
[tid] => 31
[name] => Safeway
[parents] => Array
(
[0] => 0
)
)
[1] => stdClass Object
(
[tid] => 32
[name] => Dairy
[parents] => Array
(
[0] => 31
)
)
[2] => stdClass Object
(
[tid] => 33
[name] => Milk
[parents] => Array
(
[0] => 32
)
)
)
I’m trying to create a multidimensional array where each object is a subarray of it’s parent. So, in the example above, I’m trying to output:
Array
(
[0] => stdClass Object
(
[tid] => 31
[name] => Safeway
[children] => Array
(
[tid] => 32
[name] => Dairy
[children] => Array
(
[tid] => 33
[name] => Milk
)
)
)
)
First off, what you are showing is not an multidimensional array, but an array of StdClass objects.
If it’s alright with you to make them truely arrays then this might do it:
With this you can easily find items by id with:
And to get a child:
Or all children:
EDIT
Ok, I’ve tested it now, and it seems to work fine after adding some missing semicolons.