【问题标题】:String into array with key value pair使用键值对将字符串转换为数组
【发布时间】:2020-01-28 15:27:31
【问题描述】:

我已经序列化归档了

"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 => ""

我该如何解决?

【问题讨论】:

  • 那么你有什么尝试?你的源代码在哪里?
  • 您要求的是键/值对,但列出一个编号数组作为预期结果?

标签: php arrays string serialization


【解决方案1】:

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"
}

【讨论】:

    猜你喜欢
    • 2019-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-21
    • 1970-01-01
    • 2022-11-10
    • 2016-03-09
    • 1970-01-01
    相关资源
    最近更新 更多