我有几天的问题用MySQL数据创建一个Json数组
以下是我的MySQL数据:
zi zn vn pioo pio ve station es eo
1 Zone 1 Value 01 1 199 0 1 1 0
1 Zone 1 Value 02 54 637 0 3 0 0
1 Zone 1 Value 03 55 637 1 3 0 0
2 Zone 2 Value 01 1 199 0 1 1 0
2 Zone 2 Value 03 55 637 1 3 0 0
2 Zone 2 Value 04 56 642 0 3 0 0
3 Zone 3 Value 01 1 199 0 1 1 0
3 Zone 3 Value 05 57 647 1 3 0 0
4 Zone 4 Value 05 57 647 1 3 0 0
5 Zone Test Value 02 54 637 0 3 0 0
5 Zone Test Value 03 55 637 1 3 0 0
这是我的PHP代码:
我的查询是$ query,我的SQL连接是$ con
$result = array();
$json_response = array();
$response = array();
if($stmt = $con->prepare($query)){
$stmt->execute();
$stmt->bind_result($zoneId,$zoneName,$vanneName,$piooId,$pioId,$vanne_etat,$station,$etat_station,$etat_ordre);
while($stmt->fetch()){
$json_response = [
'zi' => $zoneId,
'zn' => $zoneName,
$vanneName => [],
];
$json_response[$vanneName][] = [
'pioo' => $piooId,
'pio' => $pioId,
've' => $vanne_etat,
'es' => $etat_station,
'eo' => $etat_ordre,
];
$data = [];
foreach ($json_response as $element) {
$data[] = $element;
}
$result[]=$json_response;
}
$stmt->close();
$online = mysqli_fetch_array(mysqli_query($con, "SELECT CASE WHEN TIMESTAMP > DATE_SUB(NOW(), INTERVAL 5 MINUTE) THEN 1 ELSE 0 END AS online FROM relevés_instantanés ORDER BY ID DESC LIMIT 1")) ['online'];
$response["online"] = $online;
$response["success"] = 1;
$response["data"] = $result;
}else{
//Errors
$response["success"] = 0;
$response["message"] = mysqli_error($con);
}
mysqli_close($con);
echo json_encode($response);
像这样输出Json:
{
"online":"1",
"success":1,
"data":[
{
"zi":1,
"zn":"Zone 1",
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
},
{
"zi":1,
"zn":"Zone 1",
"Value 02":[
{
"pioo":54,
"pio":637,
"ve":0,
"es":0,
"eo":0
}
]
},
{
"zi":1,
"zn":"Zone 1",
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":2,
"zn":"Zone 2",
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
},
{
"zi":2,
"zn":"Zone 2",
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":2,
"zn":"Zone 2",
"Value 04":[
{
"pioo":56,
"pio":642,
"ve":0,
"es":0,
"eo":0
}
]
},
{
"zi":3,
"zn":"Zone 3",
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
},
{
"zi":3,
"zn":"Zone 3",
"Value 05":[
{
"pioo":57,
"pio":647,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":4,
"zn":"Zone 4",
"Value 05":[
{
"pioo":57,
"pio":642,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":5,
"zn":"Zone Test",
"Value 02":[
{
"pioo":54,
"pio":637,
"ve":0,
"es":0,
"eo":0
}
]
},
{
"zi":5,
"zn":"Zone Test",
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
}
]
}
但我期待这样的输出:(我不确定语法,但是你理解我的愿望。)
{
"online":"1",
"success":1,
"data":[
{
"zi":1,
"zn":"Zone 1"{
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
"Value 02":[
{
"pioo":54,
"pio":637,
"ve":0,
"es":0,
"eo":0
}
]
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":2,
"zn":"Zone 2"{
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
"Value 04":[
{
"pioo":56,
"pio":642,
"ve":0,
"es":0,
"eo":0
}
]
},
{
"zi":3,
"zn":"Zone 3"{
"Value 01":[
{
"pioo":1,
"pio":199,
"ve":0,
"es":1,
"eo":0
}
]
"Value 05":[
{
"pioo":57,
"pio":647,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":4,
"zn":"Zone 4"{
"Value 05":[
{
"pioo":57,
"pio":642,
"ve":1,
"es":0,
"eo":0
}
]
},
{
"zi":5,
"zn":"Zone Test"{
"Value 02":[
{
"pioo":54,
"pio":637,
"ve":0,
"es":0,
"eo":0
}
]
"Value 03":[
{
"pioo":55,
"pio":637,
"ve":1,
"es":0,
"eo":0
}
]
}
]
}
我无法隔离Zones的每个id以正确构建我的JSON ....
是否可以在while循环中使用每个ID一次?
任何帮助表示赞赏
抱歉我的英语不好..:S
乔尔
答案 0 :(得分:0)
好的,我就是这样做的。它的结构略有不同,但结构更容易。
另外我没有使用数据库,所以我将使用这个固定数组
$a = [
[
"zi" => "1",
"zn" => "Zone 1",
"vn" => "Value 01",
"pioo" => "1",
"pio" => "199",
"ve" => "0",
"station" => "1",
"es" => "1",
"eo" => "0"
], [
"zi" => "1",
"zn" => "Zone 1",
"vn" => "Value 02",
"pioo" => "54",
"pio" => "637",
"ve" => "0",
"station" => "3",
"es" => "0",
"eo" => "0"
],[
"zi" => "2",
"zn" => "Zone 2",
"vn" => "Value 04",
"pioo" => "56",
"pio" => "642",
"ve" => "0",
"station" => "3",
"es" => "0",
"eo" => "0"
]
];
如果我在foreach
以下,只要数据在每行while
,就可以用数据库中的$data
循环替换,应该没问题。
$output = [
"online" => 0,
"success" => 0,
"data" => []
];
foreach($a as $data){
//replace foreach with while from the DB
//eg. while(false != ($data = $stmt->fetch_assoc())){
$zone = "zone_{$data['zi']}";
if(!isset($output['data'][$zone])){
//setup the zone first time we see it
$output['data'][$zone] = [
"zi" => $data['zi'],
"zn" => $data['zn'],
];
}
$value = $data['vn'];
if(!isset($output['data'][$zone][$value])) $output['data'][$zone][$value] = [];
//remove these we don't want them anymore
unset($data['zi'], $data['zn'], $data['vn']);
$output['data'][$zone][$value][] = $data;
}
$output["online"] = 1;
$output["success"] = 1;
您可以在线测试here
输出
{
"online": 1,
"success": 1,
"data": {
"zone_1": {
"zi": "1",
"zn": "Zone 1",
"Value 01": [
{
"pioo": "1",
"pio": "199",
"ve": "0",
"station": "1",
"es": "1",
"eo": "0"
}
],
"Value 02": [
{
"pioo": "54",
"pio": "637",
"ve": "0",
"station": "3",
"es": "0",
"eo": "0"
}
]
},
"zone_2": {
"zi": "2",
"zn": "Zone 2",
"Value 04": [
{
"pioo": "56",
"pio": "642",
"ve": "0",
"station": "3",
"es": "0",
"eo": "0"
}
]
}
}
}
正如您所看到的,它非常接近您想要的格式,但通过添加此$zone = "zone_{$data['zi']}";
,我们可以更好地跟踪事物。
//php
$output['data'][$zone];
//json
"data": {
"zone_1"
否则我们必须首先按区域对它们进行分组,然后在第二个循环中我们可以将它们写出来。正如你所说I can not isolate each id of Zones to correctly build my JSON
解决这个问题的方法是将数据输出到按区域id分组的数组,然后循环。因此,我们必须在第1阶段构建上面的内容,然后在对它们进行分组后,正确删除zone
密钥。
正如我在评论中提到的那样,在所有这些结束时你想要的格式有一个错误。就在这里
"zn":"Zone 1"{ //<--- this bracket
"Value 01":[
{
...
所以我不知道这是不是一个错字,或者是否应该以某种方式成为结构的一部分(我只是忽略它)。
我刚刚想到了一种删除我添加的$zone
密钥的简单方法。您只需使用array_values即可删除密钥。所以如下所示,在将其编码为JSON之前添加该行。
$output['data'] = array_values($output['data']);
这会将结构更改为
{
"online": 1,
"success": 1,
"data": [
{
"zi": "1",
"zn": "Zone 1",
"Value 01": [
{
"pioo": "1",
"pio": "199",
"ve": "0",
"station": "1",
"es": "1",
"eo": "0"
}
],
"Value 02": [
{
"pioo": "54",
"pio": "637",
"ve": "0",
"station": "3",
"es": "0",
"eo": "0"
}
]
}
]
}
你可以看到在线here但是因为我们事先支持了结构,我们得到了通过密钥识别它们的优势($ zon var)