I want to loop through an array in PHP. The loop must be recursive, because I don't now how many arrays-in-arrays there are. It is for reading translations in Symfony2.
The output format is:
a.d.e
a.f.g
b.h.i
c.j.k.l.m
c.n.o
with example array:
$array = array(
'a' => array('d' => 'e', 'f' => 'g'),
'b' => array('h' => 'i'),
'c' => array(
'j' => array(
'k' => array(
'l' => 'm')),
'n' => 'o'));
I have tried the following, but this is not a final solution, but the recursion is working:
function displayArrayRecursively($array)
{
foreach ($array as $key => $value) {
if (is_array($value)) {
echo $key . '<br>';
displayArrayRecursively($value);
} else {
echo $key . '<br>' . $value . '<br>';
}
}
}
Thanks in advance!