I am able to traverse a multi-dimensional array, but I also need information about dependencies. Here is what I am trying to do. I have an array like this:
array(
'top1' => 'sth',
'top2' => array(
'sub1' => 'sth',
'sub2' => array(
'line1' => 'sth',
'line2' => 'sth'
)
)
'top3' => 'sth'
)
I am able to traverse the array to get all the keys, result is this:
array([0] => 'top1', [1] => 'top2', [2] => 'sub1', [3] => 'sub2', ...)
However, I need to know the parent of the current element. So I hope I could get something like this:
array(
[top1] => array('parent' => 0, 'id' => 1),
[top2] => array('parent' => 0, 'id' => 2),
[sub1] => array('parent' => 2, 'id' => 2.1),
[sub2] => array('parent' => 2, 'id' => 2.2),
[line1] => array('parent' => 2.2, 'id' => 2.2.1),
...
[top3] => array('parent' => 0, 'id' => 3)
)
I have been trying many ways, but couldn't always get the correct result. Can anyone solve this out? Thanks!