I have the following array from PHP:
Array
(
[0] => Array
(
[ContractExhibitsData] => Array
(
[id] => 2
[exhibit_id] => 2
[parent_id] =>
)
[children] => Array
(
[0] => Array
(
[ContractExhibitsData] => Array
(
[id] => 98
[exhibit_id] => 2
[parent_id] => 2
)
[children] => Array
(
[0] => Array
(
[ContractExhibitsData] => Array
(
[id] => 99
[exhibit_id] => 2
[parent_id] => 98
)
[children] => Array
(
)
)
[1] => Array
(
[ContractExhibitsData] => Array
(
[id] => 100
[exhibit_id] => 2
[parent_id] => 98
)
[children] => Array
(
)
)
)
)
)
)
);
It is essentially a tree array, consisting of nodes with child nodes with more data. I want to iterate over this array using a recursive function to generate an array like this:
$return = Array
(
[2] => '1.',
[98] => '1.1.',
[99] => '1.1.1.',
[100] => '1.1.2.'
);
And so forth. Basically, it will be a bunch of Key-Value pairs where the array key is the 'id' of the element in ContractExhibitsData
and the value is the numerical index.
I have the following function that I have been tinkering with, but it isn't getting me quite where I need to be.
private function getIndexes($data, $prefix = null) {
$indexes = array();
$count = 0;
if ($prefix) {
$prefix = $prefix . '.';
}
if (!empty($data['children'])) {
foreach($data['children'] as $child) {
$count++;
$indexes[$child['ContractExhibitsData']['id']] = $prefix.$count;
if (is_array($child['children']) && !empty($child['children'])) {
$subIndex = $this->getIndexes($child, $prefix.$count);
return $subIndex;
}
}
}
return $indexes;
}