Flaten array to 1D and assigning 'associative' keynames recursively
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.)