General Information:
Just to give you some background as to what I'm doing and why I'm doing it. But generally this can be used on any multi-tiered arrays. If there is a more elegant solution to this I'd love to know. It's a little messy, and doesn't take into account any 4+ generations.
--Aim: Provide an indexed array representing a navigation structure sorted on --relative positions to Smarty, Flash and other templating generating technologies.
--Source: XML file contain the navigation properties of label, link, and relative --positions.
--To Note: Some navigation items have children, this creates a tier structure to the --array.
Source Array:
Array
(
[0] => Array
(
[label] => Home
[link] => site.php?
[id] => p.1
)
[200] => Array
(
[label] => About
[link] => site.php?
[id] => p.2
[child] => Array
(
[0] => Array
(
[label] => History
[link] => site.php?
[id] => c.2.1
)
)
)
[210] => Array
(
[label] => B
[link] => site.php?
[id] => p.7
)
[250] => Array
(
[label] => Anything
[link] => _test/index.php
[id] => p.4
)
[300] => Array
(
[label] => Contact
[link] => site.php?
[id] => p.3
[child] => Array
(
[0] => Array
(
[label] => Careers
[link] => _contact/careers_frm.php
[id] => c.3.1
)
[100] => Array
(
[label] => Sales
[link] => _contact/sales_frm.php
[id] => c.3.2
[child] => Array
(
[0] => Array
(
[label] => U.S
[link] => site.php?
[id] => c.3.2.1
)
[100] => Array
(
[label] => Europe
[link] => site.php?
[id] => c.3.2.2
)
[200] => Array
(
[label] => Asia
[link] => site.php?
[id] => c.3.2.3
)
[300] => Array
(
[label] => India
[link] => site.php?
[id] => c.3.2.4
)
)
)
)
)
[400] => Array
(
[label] => New Item
[link] => site.php?
[id] => p.5
)
[500] => Array
(
[label] => A
[link] => site.php?
[id] => p.6
)
)
Code to re-index the array and sub arrays:
$arrNavStructure = $this->returnStructArray(); // returns the source array
$arrReturn = array_values($arrNavStructure); // sets the array returning the data
foreach ($arrReturn as $key=>$item) {
if (isset($item['child'])) {
$arrReturn[$key]['child'] = array_values($item['child']);
foreach ($arrReturn[$key]['child'] as $key2=>$item2) {
if (isset($item2['child'])) {
$arrReturn[$key]['child'][$key2]['child'] = array_values($item2['child']);
}
}
}
}
Returned Array:
Array
(
[0] => Array
(
[label] => Home
[link] => home.php
[id] => p.1
)
[1] => Array
(
[label] => About
[link] => site.php?
[id] => p.2
[child] => Array
(
[0] => Array
(
[label] => History
[link] => site.php?
[id] => c.2.1
)
)
)
[2] => Array
(
[label] => B
[link] => site.php?
[id] => p.7
)
[3] => Array
(
[label] => Anything
[link] => _test/index.php
[id] => p.4
)
[4] => Array
(
[label] => Contact
[link] => site.php?
[id] => p.3
[child] => Array
(
[0] => Array
(
[label] => Careers
[link] => _contact/careers_frm.php
[id] => c.3.1
)
[1] => Array
(
[label] => Sales
[link] => _contact/sales_frm.php
[id] => c.3.2
[child] => Array
(
[0] => Array
(
[label] => U.S
[link] => site.php?
[id] => c.3.2.1
)
[1] => Array
(
[label] => Europe
[link] => site.php?
[id] => c.3.2.2
)
[2] => Array
(
[label] => Asia
[link] => site.php?
[id] => c.3.2.3
)
[3] => Array
(
[label] => India
[link] => site.php?
[id] => c.3.2.4
)
)
)
)
)
[5] => Array
(
[label] => New Item
[link] => site.php?
[id] => p.5
)
[6] => Array
(
[label] => A
[link] => site.php?
[id] => p.6
)
)