# Multidimensional array iteration

Viewed   70 times

Say you have the following array:

``````\$nodes = array(
"parent node",
"parent node",
array(
"child node",
"child node",
array(
"grand child node",
"grand child node")));
``````

How would you go about transforming it to an XML string so that it looks like:

``````<node>
<node>parent node</node>
<node>parent node</node>
<node>
<node>child node</node>
<node>child node</node>
<node>
<node>grand child node</node>
<node>grand child node</node>
</node>
</node>
</node>
``````

One way to do it would be through a recursive method like:

``````function traverse(\$nodes)
{
echo "<node>";

foreach(\$nodes as \$node)
{
if(is_array(\$node))
{
traverse(\$node);
}
else
{
echo "<node>\$node</node>";
}
}

echo "</node>";
}

traverse(\$nodes);
``````

I'm looking for an approach that uses iteration, though.

4
``````<?php

\$nodes = array(
"parent node",
"parent node",
array(
"child node",
"child node",
array(
"grand child node",
"grand child node"
)
)
);

\$s = '<node>';
\$arr = \$nodes;

while(count(\$arr) > 0)
{
\$n = array_shift(\$arr);
if(is_array(\$n))
{
array_unshift(\$arr, null);
\$arr = array_merge(\$n, \$arr);
\$s .= '<node>';
}
elseif(is_null(\$n))
\$s .= '</node>';
else
\$s .= '<node>'.\$n.'</node>';
}
\$s .= '</node>';

echo \$s;

?>
``````
Monday, September 26, 2022
2

Code:

``````// Mmmm... functiony goodness
function array_to_toc (\$in, &\$out, \$level = '') {
if (!\$level) \$out = array(); // Make sure \$out is an empty array at the beginning
foreach (\$in as \$key => \$item) { // Loop items
\$thisLevel = (\$level) ? "\$level.".(\$key + 1) : (\$key + 1); // Get this level as string
\$out[\$thisLevel] = \$item['name']; // Add this item to \$out
if (isset(\$item['subs']) && is_array(\$item['subs']) && count(\$item['subs'])) array_to_toc(\$item['subs'],\$out,\$thisLevel); // Recurse children of this item
}
}

// Here is your test data (slightly modified - I think you stated it wrong in the question)
\$array = array (
0 => array (
'name' => 'test1',
'subs' => array (
0 => array (
'name' => 'test2'
),
1 => array (
'name' => 'test3',
'subs' => array (
0 => array (
'name' => 'test4'
)
)
)
)
),
1 => array (
'name' => 'test5'
)
);

// \$result is passed by reference and will hold the output after the function has run
\$result = array();
array_to_toc(\$array, \$result);

print_r(\$result);
``````

Output:

``````Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
``````

Demo

EDIT

These two (plus one supporting) functions allow you add and remove chapters from the input array by chapter reference. Then, you can recalculate the TOC from the new structure.

``````function chapter_exists (\$array, \$chapterId) {
\$chapterParts = explode('.',\$chapterId);
foreach (\$chapterParts as &\$chapter) \$chapter--;
\$lastId = array_pop(\$chapterParts);
return eval('return isset(\$array['.implode("]['subs'][",\$chapterParts).((count(\$chapterParts)) ? "]['subs'][" : '')."\$lastId]);");
}

function add_chapter (&\$array, \$chapterId, \$item) {
\$chapterParts = explode('.',\$chapterId);
foreach (\$chapterParts as &\$chapter) \$chapter--; // Decrement all the values
\$lastId = array_pop(\$chapterParts);
if (count(\$chapterParts) && !chapter_exists(\$array, implode('.',\$chapterParts))) return FALSE; // Return FALSE if the level above the chapter we are adding doesn't exist
if (chapter_exists(\$array, \$chapterId)) { // See if the chapter reference already exists
eval('array_splice(\$array'.((count(\$chapterParts)) ? '['.implode("]['subs'][",\$chapterParts)."]['subs']" : '').",\$lastId,0,array(\$item));"); // Insert an item
} else {
eval('\$array['.implode("]['subs'][",\$chapterParts).((count(\$chapterParts)) ? "]['subs'][" : '')."\$lastId] = \$item;"); // Insert an item
}
return TRUE;
}

function remove_chapter (&\$array, \$chapterId) {
\$chapterParts = explode('.',\$chapterId);
foreach (\$chapterParts as &\$chapter) \$chapter--; // Decrement all the values
\$lastId = array_pop(\$chapterParts);
return (chapter_exists(\$array, \$chapterId)) ? eval('\$removed = array_splice(\$array'.((count(\$chapterParts)) ? '['.implode("]['subs'][",\$chapterParts)."]['subs']" : '').",\$lastId,1); return array_shift(\$removed);") : FALSE;
}
``````

The best way to demonstrate how they work is with an example. Say we start with the array structure above, which is held in a variable called `\$structure`. As we know, our resulting TOC array looks like this:

``````Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
``````

Now, we decide we want to remove chapter `1.2` and all it's sub-chapters - we can do this:

``````// Remove the chapter from \$structure
remove_chapter(\$structure, '1.2');
// recalculate the TOC
array_to_toc(\$structure, \$result2);

print_r(\$result2);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test2
[2] => test5
)
*/
``````

Now lets say we want to add a chapter called `test6` as chapter `1.1`, and `test2` will be re-indexed to `1.2` - we'll be working with the result of the above example for this one:

``````// Add the new chapter to \$structure
// recalculate the TOC
array_to_toc(\$structure, \$result3);

print_r(\$result3);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test6
[1.2] => test2
[2] => test5
)
*/
``````

OK, seems fairly simple. But what if we wanted to move a sub-chapter, so it was at the top level of the tree? Let's go back to our original version of `\$structure` to demonstrate this - we'll move chapter `1.2`, so that it is now chapter `3`:

``````/*
A quick reminder of what we are starting with:
Array
(
[1] => test1
[1.1] => test2
[1.2] => test3
[1.2.1] => test4
[2] => test5
)
*/

// Remove the chapter from \$structure - this time, we'll catch the items we remove in a variable
\$removed = remove_chapter(\$structure, '1.2');
// Add it again, only this time as chapter 3

// recalculate the TOC
array_to_toc(\$structure, \$result4);

print_r(\$result4);
/*
Outputs:
Array
(
[1] => test1
[1.1] => test2
[2] => test5
[3] => test3
[3.1] => test4
)
*/
``````

Hopefully I've explained it well enough there.

`chapter_exists()` returns a boolean. Fairly self explanatory as to what it means, if feel. Pass the `\$structure` array as the first parameter, and the chapter ID you want to check as the second. This function is required, as it is used by the other two internally.

`add_chapter()` returns a boolean, so you can test whether the operation was successful. It will fail if the parent of the chapter doesn't exist - for example, if you try to add `1.2.1` when `1.2` hasn't been defined, it won't work. If you add a chapter that already exists, all the chapter numbers at that level will be shifted up by 1.

`remove_chapter()` will return the item that was removed on success (i.e. an array) or boolean `FALSE` on failure - it will fail if you try and remove a chapter that doesn't exist.

NB: I had to make heavy use of `eval()` for this, in order to accommodate for arbitrary level depth. I hate to use it, but I couldn't think of any other way - if anyone reading this has any bright ideas about alternative approaches (preferably that don't involve some nightmarish looping structure), please let me know...

Saturday, August 27, 2022
5

Because `Array.ToString()` does not return the contents of the array, it returns the type name, and `Console.WriteLine` implicitly calls `ToString()` on each object you send it as a parameter.

This has no regard to the fact that the array is part of a multi-dimensional array, it is simply the way the CLR developers chose to (or rather, chose not to) implement `ToString()` on `System.Array`.

Wednesday, August 10, 2022
5

It's clear you're using numpy. With numpy you can just do:

``````for cell in self.cells.flat:
do_somethin(cell)
``````
Tuesday, October 11, 2022
2

Here's what you need. I have commented as necessary:

``````function permutations(array \$array)
{
switch (count(\$array)) {
case 1:
// Return the array as-is; returning the first item
// of the array was confusing and unnecessary
return \$array;
break;
case 0:
throw new InvalidArgumentException('Requires at least one array');
break;
}

// We 'll need these, as array_shift destroys them
\$keys = array_keys(\$array);

\$a = array_shift(\$array);
\$k = array_shift(\$keys); // Get the key that \$a had
\$b = permutations(\$array);

\$return = array();
foreach (\$a as \$v) {
if(is_numeric(\$v))
{
foreach (\$b as \$v2) {
// array(\$k => \$v) re-associates \$v (each item in \$a)
// with the key that \$a originally had
// array_combine re-associates each item in \$v2 with
// the corresponding key it had in the original array
// Also, using operator+ instead of array_merge
// allows us to not lose the keys once more
\$return[] = array(\$k => \$v) + array_combine(\$keys, \$v2);
}
}
}

return \$return;
}
``````

See it in action.

By the way, calculating all the permutations recursively is neat, but you might not want to do it in a production environment. You should definitely consider a sanity check that calculates how many permutations there are and doesn't allow processing to continue if they are over some limit, at the very least.

Sunday, October 23, 2022