简体   繁体   English

如何在 PHP 中将 JSON 字符串 [{"name":"fieldname":"value":"fieldvalue"}] 转换为数组

[英]How do I convert a JSON string [{"name":"fieldname":"value":"fieldvalue"}] into array IN PHP

I'm useing PHP.我正在使用 PHP。 my string is:我的字符串是:

[{"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"}]

I also need to combine the like values like this:我还需要像这样组合类似的值:

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

so the array field would be Array ([childdates[0].datesAttending] => 24,26,25 )所以数组字段将是 Array ([childdates[0].datesAttending] => 24,26,25 )

And my Array would be:我的数组将是:

 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 )

If you have the JSON in a string variable like,如果您在字符串变量中有 JSON,例如,

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

Then use: json_decode()然后使用: json_decode()

$my_json_array = json_decode($my_json_string );

Have a look at: http://php.net/manual/en/ref.json.php看看: http : //php.net/manual/en/ref.json.php

All the json functions are found there and described quite simple.所有的 json 函数都可以在那里找到并且描述得非常简单。

and

How to convert JSON string to array 如何将 JSON 字符串转换为数组

Once you have the arrays you can look into:拥有数组后,您可以查看:

There are quite a lot of answers on here that explain just that.这里有很多答案可以解释这一点。

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

print_r(json_decode($json));

use json_decode() function to convert in into array.使用json_decode()函数将 in 转换为数组。

You can also use code like this to convert your json into array您还可以使用这样的代码将您的 json 转换为数组

$myArray = json_decode($decodedText, true); $myArray = json_decode($decodedText, true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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