我有一个数组`$ A”,包括以下数据:PHP:如何生成这个JSON?
[0]=>'text',
[1]=>'text1, text2, text3'
我想生成该数据的JSON对象,如下:
'{"TestingApplications": [
{ "ApplicationName": "text" },
{ "ApplicationName": "text1" },
{ "ApplicationName": "text2" },
{ "ApplicationName": "text3" }
]
}'
我做了什么:
<?php
$a=array(0=>"text", 1=>"text1, text2, text3");
$r = array();
foreach($a as $key => $val){
if (strpos($val,',') !== false) {
$v = explode(',', $val);
foreach($v as $k => $l){
array_push($r, $l);
}
} else{
array_push($r, $val);
}
}
那么,如何使用存储在$r
中的数据生成所需的JSON呢?
完整答案是已接受的答案。 – Mohammad