字符串与键值对组合成数组

时间:2019-09-28 18:27:33

标签: php arrays string serialization

我已序列化提交

"date_from=&time_from=&date_to=&time_to=&email=&title=&text=&date_from_full=&date_to_full=&sortID=SortiddReverse&"

我需要将键值对转换为数组的方法我尝试了.explode,但是没有按我的要求工作

 0 => "date_from="
  1 => "time_from="
  2 => "date_to="
  3 => "time_to="
  4 => "email=1"
  5 => "title=1111"
  6 => "text=1111"
  7 => "date_from_full="
  8 => "date_to_full="
  9 => "sortID=SortiddReverse"
  10 => ""

我该如何解决?

1 个答案:

答案 0 :(得分:0)

PHP中已经有名为parse_str()的函数可以检索您要求的键/值对。

$string = "date_from=&time_from=&date_to=&time_to=&email=&title=&text=&date_from_full=&date_to_full=&sortID=SortiddReverse&";

$array = [];
parse_str($string, $array):

并会输出

array(10) {
  'date_from' =>
  string(0) ""
  'time_from' =>
  string(0) ""
  'date_to' =>
  string(0) ""
  'time_to' =>
  string(0) ""
  'email' =>
  string(0) ""
  'title' =>
  string(0) ""
  'text' =>
  string(0) ""
  'date_from_full' =>
  string(0) ""
  'date_to_full' =>
  string(0) ""
  'sortID' =>
  string(14) "SortiddReverse"
}