I've got a multidimensional array of which I can't know the depth. The array could for example look like this:
$array = array(
1 => array(
5 => array(
3 => 'testvalue1'
)
),
2 => array(
6 => 'testvalue2'
),
3 => 'testvalue3',
4 => 'testvalue4',
);
With this array I want to create a table of contents. That means the keys need to be preserved as I'm using them as "chapter numbers". For example, "testvalue1" is in chapter 1.5.3.
Now I want to walk through the array while preserving all keys - not using array_walk_recursive as the keys containing another array are dropped (correct?) and preferably not using nested foreach loops considering the speed.
Any suggestions how I should do this? Thanks in advance.
Now I want to walk through the array while preserving all keys - not using array_walk_recursive as the keys containing another array are dropped (correct?) and preferably not using nested foreach loops considering the speed.
Any suggestions how I should do this? Thanks in advance.
PS: For my script it doesn't matter if the keys are strings ("1" instead of 1) or integers, if having strings as key will make array_walk_recursive preserve them.
You can iterate over your array with a help of a stack to build your toc.
You can iterate over your array with a help of a stack to build your toc.
$stack = &$array;
$separator = '.';
$toc = array();
while ($stack) {
list($key, $value) = each($stack);
unset($stack[$key]);
if (is_array($value)) {
$build = array($key => ''); # numbering without a title.
foreach ($value as $subKey => $node)
$build[$key . $separator . $subKey] = $node;
$stack = $build + $stack;
continue;
}
$toc[$key] = $key. ' ' . $value;
}
print_r($toc);
Output:
Array
(
[1] => 1
[1.5] => 1.5
[1.5.3] => 1.5.3 testvalue1
[2] => 2
[2.6] => 2.6 testvalue2
[3] => 3 testvalue3
[4] => 4 testvalue4
)
You can additionally handle the level as well if you need to, but that was not clear from your question.
array_walk_recursive
does not work, because it won't give you the keys of the parent element(s). See this related question as well: Transparently flatten an array, it has a good answer and is helpful for more generic cases as well.
0 comments:
Post a Comment