简体   繁体   中英

php : convert serialized to array

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?

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',
  )
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM