>我尝试处理具有复杂结构的JSON响应,但多次尝试后我不知道如何管理它。
JSON 响应必须如下所示:
"note": {
"vote": 3,
"items":[
{
"value": 1,
"touchArea": {
"x": 122,
"y": 173,
"w": 89,
"h": 89
}
},
{
"value": 2,
"touchArea": {
"x": 122,
"y": 283,
"w": 89,
"h": 89
}
},
{
"value": 3,
"touchArea": {
"x": 122,
"y": 390,
"w": 89,
"h": 89
}
}
]
注意:"vote"是数组的最大值
作为源,我请求 MYSQL 并得到这个数组 ($touch):
Array ( [0] => V:1,X:122,Y:173,W:89,H:89 [1] => V:2,X:122,Y:283,W:89,H:89 [2] => V:3,X:122,Y:390,W:89,H:89 )
我的问题是:如何使用循环从PHP生成此JSON响应,在此示例中,我们只有3个值,但可能会更多。
您可以使用以下命令编辑和重新保存 json 文件或字符串中的数据。
<?php
$sInFile = 'in2.json';
$sOutFile = 'out2.json';
$aDevelopers = array();
$aArtists = array();
$aCooks = array();
$sRaw = file_get_contents( $sInFile );
var_dump( $sRaw );
// object
$oData = json_decode( $sRaw );
//array
$aData = json_decode( $sRaw, 1 );
$aNote = $aData[ 'note' ];
$aItems = $aNote[ 'items' ];
var_dump( $aData );
$iCountData = count( $aItems );
// Loops through items and set new values.
for( $i = 0; $i < $iCountData; ++$i )
{
$aItem = $aItems[ $i ];
$aItem[ 'value' ] = 'newvalue';
$aItem[ 'touchArea' ][ 'x' ] = 3455;
$aItem[ 'touchArea' ][ 'y' ] = 43;
$aItems[ $i ] = $aItem;
}
$aNote[ 'items' ] = $aItems;
$aData[ 'note' ] = $aNote;
var_dump( $aData );
$sNewJson = json_encode( $aData );
file_put_contents( $sOutFile, $sNewJson );
?>
假设来自MySQL的结果集是一个字符串数组,正如您的问题中所暗示的那样,您需要迭代此结果集,在,
上拆分字符串,然后在:
上拆分字符串,然后将数据处理成所需的结构,最后转换为json。
鉴于您提供的信息,以下内容应该适合您:
<?php
// your initial result set from mysql - an array of strings
$touch = [
'V:1,X:122,Y:173,W:89,H:89',
'V:2,X:122,Y:283,W:89,H:89',
'V:3,X:122,Y:390,W:89,H:89',
];
// map over each element in your result set...
$items = array_map(function($item) {
$array = [];
// first, break each string on `,` to
// get an array of elements; e.g:
// ['V:1', 'X:122', 'Y:173', 'W:89', 'H:89'] etc.
$itemElements = explode(',', $item);
// then, iterate over these elements...
foreach ($itemElements as $itemElement) {
// explode on `:` and assign as a key value pair
list($key, $value) = explode(':', $itemElement);
// then, check the key and add the
// value to the array as appropriate
if ('V' === $key) {
$array['value'] = $value;
} else {
$array['touchArea'][strtolower($key)] = $value;
}
}
return $array;
}, $touch);
// then, build the `$note` array...
$note = [
'note' => [
'vote' => count($items),
'items' => $items,
]
];
// finally, json encode
echo json_encode($note, JSON_PRETTY_PRINT);
这会产生:
{
"note": {
"vote": 3,
"items": [
{
"value": "1",
"touchArea": {
"x": "122",
"y": "173",
"w": "89",
"h": "89"
}
},
{
"value": "2",
"touchArea": {
"x": "122",
"y": "283",
"w": "89",
"h": "89"
}
},
{
"value": "3",
"touchArea": {
"x": "122",
"y": "390",
"w": "89",
"h": "89"
}
}
]
}
}
希望这对:)有所帮助
具有 array_map
、explode
、array_column
和 max
函数的解决方案:
$touch = ["V:1,X:122,Y:173,W:89,H:89", "V:2,X:122,Y:283,W:89,H:89", "V:3,X:122,Y:390,W:89,H:89"];
$result_arr = ["note" => ["vote" => 0, "items" => []]]; // basic structure
array_map(function($v) use (&$result_arr){
$arr = explode(",", $v);
$result = [];
foreach ($arr as $value) {
$new_arr = explode(":", $value);
$key = strtolower($new_arr[0]);
if ($key == "v"){
$result["value"] = $new_arr[1];
} else {
$result["touchArea"][$key] = $new_arr[1];
}
}
$result_arr["note"]["items"][] = $result;
}, $touch);
// getting max vote
$result_arr["note"]["vote"] = max(array_column($result_arr["note"]["items"], "value"));
$final_json = json_encode($result_arr, JSON_PRETTY_PRINT);