Flatten a PHP array

Posted by deadkarma on Stack Overflow See other posts from Stack Overflow or by deadkarma
Published on 2010-04-23T22:33:39Z Indexed on 2010/04/23 22:43 UTC
Read the original article Hit count: 354

Filed under:
|
|

Say I have a form with these fields, and cannot rename them:

<input type="text" name="foo[bar]" />
<input type="text" name="foo[baz]" />
<input type="text" name="foo[bat][faz]" />

When submitted, PHP turns this into an array:

Array
(
    [foo] => Array
        (
            [bar] => foo bar
            [baz] => foo baz
            [bat] => Array
                (
                    [faz] => foo bat faz
                )

        )

)

What methods are there to convert or flatten this array into a data structure such as:

Array
(
    [foo[bar]] => foo bar
    [foo[baz]] => foo baz
    [foo[bat][faz]] => foo bat faz
)

© Stack Overflow or respective owner

Related posts about php

Related posts about array