gpt4 book ai didi

php - 如何在 while 循环中对 ID 进行分组(对于 php json_encode)

转载 作者:行者123 更新时间:2023-11-29 17:48:46 25 4
gpt4 key购买 nike

我在用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
}
]
}
]
}

我无法隔离区域的每个 id 来正确构建我的 JSON ....

是否可以在 while 循环中使用每个 ID 一次?

感谢任何帮助

抱歉我的英语不好..:S

乔尔

最佳答案

好吧,我就是这样做的。这是一种略有不同的结构,但更容易构建。

此外,我没有使用数据库,因此我将使用这个固定数组

 $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"
]
];

只要数据位于每行 $data 中,您就可以将下面的 foreach 替换为数据库中的 while 循环,应该没问题。

  $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"

否则我们必须首先按区域对它们进行分组,然后在第二个循环中我们可以将它们写出来。正如您所说,我无法隔离区域的每个 id 来正确构建 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)识别它们的优势

关于php - 如何在 while 循环中对 ID 进行分组(对于 php json_encode),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49581335/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com