PHP multidimensional array key collapse -
i'd collapse multidimensional array that
$arr = array( 'first' => array( 'second' => array( 0 => 'foo', 1 => 'bar' ) ) );
would collapse to
$arr = array( 'first[second][0]' => 'foo', 'first[second][1]' => 'bar' );
what i'm trying restore multipart/form-data
$_post
array original request body, this:
------webkitformboundaryxioccklmg4al6vbh content-disposition: form-data; name="first[second][0]" foo ------webkitformboundaryxioccklmg4al6vbh content-disposition: form-data; name="first[second][1]" bar ------webkitformboundaryxioccklmg4al6vbh--
try code:
$arr = array( 'first' => array( 'second' => array( 0 => 'foo', 1 => 'bar' ) ) ); $result = array(); function processlevel(&$result, $source, $previous_key = null) { foreach ($source $k => $value) { $key = $previous_key ? "{$previous_key}[{$k}]" : $k; if (!is_array($value)) { $result[$key] = $value; } else { processlevel($result, $value, $key); } } } processlevel($result, $arr); var_dump($result); die();
it outputs:
array(2) { ["first[second][0]"] => string(3) "foo" ["first[second][1]"] => string(3) "bar" }
Comments
Post a Comment