如何将JSON字符串[{“name”:“fieldname”:“value”:“fieldvalue”}]转换为数组IN PHP

时间:2015-09-08 12:06:39

标签: php arrays json

我正在使用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"}, 

所以数组字段是数组([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 )

4 个答案:

答案 0 :(得分:1)

如果您在字符串变量中有JSON,

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

然后使用:json_decode()

$my_json_array = json_decode($my_json_string );

答案 1 :(得分:0)

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

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

How to convert JSON string to array

获得阵列后,您可以查看:

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

答案 2 :(得分:0)

$json = '{"name":"abc","age":"20","lastname":"xyz"}';

print_r(json_decode($json));

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

答案 3 :(得分:0)

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

$ myArray = json_decode($ decodingText,true);