Flaten array to 1D and assigning 'associative' keynames recursively I would like to convert ($data):

Array
(
    [login] =&gt; Log in
    [logout] =&gt; Log out

    [label] =&gt; Array
        (
            [email] =&gt; test@test.com 
            [name] =&gt; Some name
        )

    [controllers] =&gt; Array
        (
            [page] =&gt; Array
                (
                    [title_new] =&gt; New page 
                    [title_edit] =&gt; Edit page
                    [submit_button] =&gt; Submit page
                )
        )
)


To ($result):

Array
(
    [login] =&gt; Log in
    [logout] =&gt; Log out
    [label[email]] =&gt; test@test.com
    [label[name]] =&gt; Some name
    [controllers[page][title_new]] =&gt; New page
    [controllers[page][title_edit]] =&gt; Edit page
    [controllers[page][submit_button]] =&gt; Submit page
 )


The following code works "fine":

$data = some_data;
$result = array();

foreach ($data as $k =&gt; $v) {
  if (is_array($v))
    foreach ($v as $_k =&gt; $_v)
      if (is_array($_v))
        foreach ($_v as $__k =&gt; $__v)
          if (is_array($__v))
            foreach ($__v as $___k =&gt; $___v)
              $result[$k.'['.$_k.']['.$__k.']['.$___k.']'] = $___v;
          else
            $result[$k.'['.$_k.']['.$__k.']'] = $__v;
      else
        $result[$k.'['.$_k.']'] = $_v;
  else
    $result[$k] = $v;
}


The code work nicely for four levels, and after that it returns "Array" as the value. I think I need recursion, but I would like for you to review my current code, and optionally present me with a solution to go even deeper.
So I probably need some kind of recursion, but I have no clue how to get around that with PHP


Recursion in PHP is the same as basically any language (well... at a very high level).

I'm very torn on if I think your question is on topic or not, but it's been a long time since I've written anything recursive, and I'm a shameless rep-whore, so here's my go:

function flatten(array $arr, $prefix = '')
{
    $out = array();
    foreach ($arr as $k =&gt; $v) {
        $key = (!strlen($prefix)) ? $k : "{$prefix}[{$k}]";
        if (is_array($v)) {
            $out += flatten($v, $key);
        } else {
            $out[$key] = $v;
        }
    }
    return $out;
}


Or, if you don't like the magical second param:

function _flatten(array &amp;$out, array $arr, $prefix)
{
    foreach ($arr as $k =&gt; $v) {
        $key = (!strlen($prefix)) ? $k : "{$prefix}[{$k}]";
        if (is_array($v)) {
            _flatten($out, $v, $key);
        } else {
            $out[$key] = $v;
        }
    }
}

function flatten(array $arr)
{

    $flat = array();
    _flatten($flat, $arr, '');
    return $flat;

}


You could also optimize a bit and make a lot of the things in there references (the for loop values and function params).  I tend to avoid references in PHP though unless I have an extremely strong reason for them.  (If you plan on using this function on arrays larger than a few hundred elements, that may begin to enter into strong reason land.)

(And flatten is a horrible name, but... yeah.)