【问题标题】:How do I convert a JSON string [{"name":"fieldname":"value":"fieldvalue"}] into array IN PHP如何在 PHP 中将 JSON 字符串 [{"name":"fieldname":"value":"fieldvalue"}] 转换为数组
【发布时间】:2015-09-08 12:06:38
【问题描述】:

我正在使用 PHP。我的字符串是:

[{"name":"emailaddress","value":"me@you.com"},
{"name":"familyname","value":"kasjhdf"},{"name":"mothersname","value":"kjh"},
{"name":"motherscellphone","value":"23423423423"},
{"name":"fathersname","value":""},{"name":"fatherscellphone","value":""},
{"name":"childdates1.datesAttending","value":"24"},
{"name":"childdates1.datesAttending","value":"26"},
{"name":"childdates1.datesAttending","value":"25"},
{"name":"childname1.child_name","value":"asd"},
{"name":"childdob1.child_dob","value":"09/08/2015"},
{"name":"childallergies1.child_allergies","value":"asdf"},
{"name":"childinstructions1.child_instructions","value":"a"},
{"name":"childbelongings1.belongings","value":"sawef"},
{"name":"datesAttending","value":""},{"name":"child_name","value":""},
{"name":"child_dob","value":""},{"name":"child_allergies","value":""},
{"name":"child_instructions","value":""},{"name":"belongings","value":""},
{"name":"groupid","value":"zzzz"},{"name":"job_date_id_count","value":"3"},
{"name":"child_count","value":"1"},{"name":"job_group_name_id","value":"83"},
{"name":"job_group_email_id","value":"55"},{"name":"willbill","value":"0"},
{"name":"form_submitted","value":"1"}]

我还需要像这样组合类似的值:

{"name":"childdates1.datesAttending","value":"24"},
{"name":"childdates1.datesAttending","value":"26"},
{"name":"childdates1.datesAttending","value":"25"}, 

所以数组字段将是 Array ([childdates[0].datesAttending] => 24,26,25 )

我的数组是:

 Array ( [emailaddress] => me@you.com [familyname] => skadjhf [mothersname] 
=> kjh [motherscellphone] => 234234234343 [fathersname] => [fatherscellphone] => 
234234234324 [child[0] => [0.child_name] => lkjasdf [child[1] => [1.child_name] 
=> sdfwef [child_name] => [child_dob] => [child_allergies] => 
[child_instructions] => [belongings] => [groupid] => zzzz [job_date_id_count] => 
2 [child_count] => 1 [job_group_name_id] => 83 [job_group_email_id] => 55 
[willbill] => 0 [form_submitted] => 1 )

【问题讨论】:

  • 在 javascript 中是 json.parse(),在 php 中是 json_decode()
  • 您使用哪种语言?
  • 语言在标签中。
  • $result = json_decode($json_string,true);

标签: php arrays json


【解决方案1】:

如果您在字符串变量中包含 JSON,例如,

$my_json_string = '[{"name":"emailaddress","value":"me@you.com"}...';

然后使用:json_decode()

$my_json_array = json_decode($my_json_string );

【讨论】:

    【解决方案2】:

    看看:http://php.net/manual/en/ref.json.php

    所有的 json 函数都在那里找到并且描述得非常简单。

    How to convert JSON string to array

    一旦你有了数组,你就可以查看:

    这里有很多答案可以解释这一点。

    【讨论】:

      【解决方案3】:
      $json = '{"name":"abc","age":"20","lastname":"xyz"}';
      
      print_r(json_decode($json));
      

      使用json_decode()函数转换成数组。

      【讨论】:

        【解决方案4】:

        您也可以使用这样的代码将您的 json 转换为数组

        $myArray = json_decode($decodedText, true);

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-06-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2011-05-29
          • 1970-01-01
          • 2016-04-28
          相关资源
          最近更新 更多