[英]PHP: how to generate this JSON?
I have an array `$a', includes the following data: 我有一个数组$ a,包含以下数据:
[0]=>'text',
[1]=>'text1, text2, text3'
I want to generate a JSON object from this data, as follow: 我想从此数据生成JSON对象,如下所示:
'{"TestingApplications": [
{ "ApplicationName": "text" },
{ "ApplicationName": "text1" },
{ "ApplicationName": "text2" },
{ "ApplicationName": "text3" }
]
}'
What I did: 我做了什么:
<?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);
}
}
So, how I can generate the required JSON using the data stored in $r
? 因此,如何使用$r
存储的数据生成所需的JSON?
I'd start by creating the basic structure of the json object: 我将从创建json对象的基本结构开始:
$temp = [
'TestingApplications' => [],
];
Next, iterate over the data you have, and explode
all of the comma-containing strings like you're doing right now: 接下来,遍历您拥有的数据,并像您现在正在执行的那样explode
所有包含逗号的字符串:
foreach ($a as $value) {
$exploded = explode(',', $value);//no need to check for comma's
foreach ($exploded as $name) {
//perhaps consider calling trim on $name here
$temp['TestingApplications'][] = ['ApplicationName' => $name];
}
}
Then finally, json_encode
the lot: 然后最后, json_encode
很多:
return json_encode($temp);
Although truth be told: I'd really try to change the way the data is provided to my code. 尽管说实话:我确实会尝试更改将数据提供给代码的方式。 If all the string values should be treated the same way, it's totally unacceptable that sometimes, they're assigned to a distinct key (in the $a
array), but that on other occasions, they're in a comma separated string. 如果所有字符串值都应以相同的方式处理,则有时将它们分配给不同的键(在$a
数组中)是完全不可接受的,但在其他情况下,它们位于逗号分隔的字符串中。
This, to me at least, looks like an XY problem 至少对我来说,这看起来像是XY问题
Try as below : 尝试如下:
<?php
$a=array(0=>"text", 1=>"text1, text2, text3");
$r = array();$final = array();
foreach($a as $key => $val){
if (strpos($val,',') >= 0) {
$v = explode(',', $val);
foreach($v as $k => $l){
$r[]['ApplicationName']= "$l";
}
}
}
$final['TestingApplications'] = $r;
echo json_encode($final);
?>
You can try json_encode . 您可以尝试json_encode 。 This will returns the JSON representation of a value 这将返回值的JSON表示形式
For example: 例如:
$arr = array(0=>"text", 1=>"text1, text2, text3");
echo json_encode($arr);
Hope this help you well. 希望这对您有帮助。
json_encode(); json_encode(); is used to convert data in json from array in php. 用于从php中的数组转换json中的数据。
$array = array('TestingApplications'=>array( "appname1"=>"text1","appname2"=>"text2", "appname3"=>"text3"));
echo json_encode($array);
Assuming you have this source: 假设您有以下来源:
$sourceArray = array(
'text',
'text1, text2, text3',
);
You need to build the right PHP array before you convert it to JSON. 您需要先构建正确的PHP数组,然后才能将其转换为JSON。 I propose to do so in a recursive way, while trimming the empty spaces too: 我建议以递归的方式这样做,同时也修剪空白空间:
// get a flat array
$tmpArray = array_map(function($array) {
return explode(',', $array);
}, $sourceArray);
$flatArray = array();
array_walk_recursive($tmpArray, function($a) use (&$flatArray) {
$flatArray[] = trim($a);
});
$toJsonArray = array('TestingApplications' => array());
foreach ($flatArray as $val) {
$toJsonArray['TestingApplications'][] = array(
'ApplicationName' => $val,
);
}
Then you just have to encode it to JSON: echo json_encode($toJsonArray);
然后,您只需将其编码为JSON: echo json_encode($toJsonArray);
To generate required JSON Object, array should be like following: 要生成所需的JSON对象,数组应如下所示:
$arr = array(
'TestingApplications' => array(
array('ApplicationName' => 'text'),
array('ApplicationName' => 'text1'),
array('ApplicationName' => 'text2'),
array('ApplicationName' => 'text3'),
)
); );
and then encode this array to json as: echo json_encode($arr);
然后将此数组编码为json: echo json_encode($arr);
It will return required JSON Object 它将返回所需的JSON对象
try this. 尝试这个。
$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){
$final['ApplicationName']=$l;
$final_arr[]=$final;
}
} else{
$final['ApplicationName']=$val;
$final_arr[]=$final;
}
}
$result['TestingApplications']=$final_arr;
echo json_encode($result);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.