Flatten a PHP array
- by deadkarma
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
)