Here is my data returned from a CMS function:
array (
0 =>
stdClass::__set_state(array(
'term_id' => '31',
'parent' => '26'
)),
1 =>
stdClass::__set_state(array(
'term_id' => '3',
'parent' => '0'
)),
2 =>
stdClass::__set_state(array(
'term_id' => '32',
'parent' => '26'
)),
3 =>
stdClass::__set_state(array(
'term_id' => '33',
'parent' => '26'
)),
4 =>
stdClass::__set_state(array(
'term_id' => '34',
'parent' => '26'
)),
5 =>
stdClass::__set_state(array(
'term_id' => '26',
'parent' => '3'
)),
I need to convert the above to the following format:
Array
(
[0] => Array
(
3
)
[1] => Array
(
26
)
[2] => Array
(
31
32
33
34
)
)
So let me explain. Each item in the source is a term. Each term has an ID (term_id)
and a parent (parent)
. If the parent == 0 it doesn't have a parent and is level 0. Any child items of level 0 are level 1 and so on.
What I want to return is an array that holds all the levels and its ID's at that level.
Be aware that this is just sample data and there could be many more levels and any number of ID's on each level.
So using PHP how do I achieve what I'm after?