Ad

Php : Convert Serialized To Array

- 1 answer

I have a form sending a post request to server . there is a hidden input in form containing a serialized string value :
"menuItem[2]=null&menuItem[4]=2&menuItem[6]=4&menuItem[5]=2&menuItem[7]=null&menuItem[3]=null"

I'm trying to convert this string to an array using php:
[ 2 => null, 4 => 2, 6 => 4 , ...]
is there any neat way to do that?

Ad

Answer

You can just use parse_str.

parse_str('menuItem[2]=null&menuItem[4]=2&menuItem[6]=4&menuItem[5]=2&menuItem[7]=null&menuItem[3]=null', $arr);
var_dump($arr);

Will result in:

array (
  'menuItem' => 
    array (
      2 => 'null',
      4 => '2',
      6 => '4',
      5 => '2',
      7 => 'null',
      3 => 'null',
  )
)
Ad
source: stackoverflow.com
Ad