gpt4 book ai didi

php - 从 MySQL/PHP 修复 JSON 格式

转载 作者:行者123 更新时间:2023-12-04 10:57:21 25 4
gpt4 key购买 nike

在下面的两个版本的代码中,第一个产生了结果,但请注意“文本”数组的末尾没有结束括号,因为它应该有。来自其他代码示例 SEEMS 的第二个输出应该可以工作,但它完全失败了。我哪里做错了?

当我这样做时;

foreach($db_found->query($sql) as $row) {
$json_array[] =

array('start_date' =>
array('minute' => $row[min], 'hour' => $row[hour],
'month' => $row[mo], 'day' => $row[day], 'year' => $row[yr]),
'text' =>
array('text' => $row[text],
'group' =>
array('group' => $row[callsign])))
;
}
$data = array("events" => $json_array);
echo json_encode($data);

我明白了:
    {
"events":[
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"BILL SWEENEY Opened the net from 65.255.143.178 on 146.655MHz, PL94.8Hz",
"group":{
"group":"W0WTS"
}
}
},
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"Peculiar: Clear, 19.9F, wind: N @ 15, humidity: 54%",
"group":{
"group":"GENCOMM"
}
}
}
]
}

但我需要的是这个:
{
"events":[
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"BILL SWEENEY Opened the net from 65.255.143.178 on 146.655MHz, PL94.8Hz"
},
"group":{
"group":"W0WTS"
}
},
{
"start_date":{
"minute":"42",
"hour":"18",
"month":"11",
"day":"11",
"year":"2019"
},
"text":{
"text":"Peculiar: Clear, 19.9F, wind: N @ 15, humidity: 54%"
},
"group":{
"group":"GENCOMM"
}
}
]
}

我也试过:
 foreach($db_found->query($sql) as $row) {
$json_array[] =

array('start_date' =>
array('minute' => $row[min], 'hour' => $row[hour],
'month' => $row[mo], 'day' => $row[day], 'year' => $row[yr]),
array('text' =>
array('text' => $row[text]),
array('group' =>
array('group' => $row[callsign]))
;
}
$data = array("events" => $json_array);
echo json_encode($data);

但这根本行不通。我究竟做错了什么。

最佳答案

因此,我开始美化您的 JSON 和 PHP,然后重新格式化您的代码,以便更容易理解所需的数据层次结构以及编码数据层次结构。
这是重新格式化:

<?php
foreach ($db_found->query($sql) as $row) {
$json_array[] =
[
'start_date' =>
[
'minute' => $row['min'],
'hour' => $row['hour'],
'month' => $row['mo'],
'day' => $row['day'],
'year' => $row['yr']
],
'text' =>
[
'text' => $row['text'],
'group' =>
[
'group' => $row['callsign']
]
]
];
}
$data = ["events" => $json_array];
echo json_encode($data);

?>

您可以看到“group”数组位于“text”数组内。在重新格式化你想要的 JSON 之后,我认为这就是你要找的:

以及产生我认为您正在寻找的输出的代码:
<?php
foreach ($db_found->query($sql) as $row) {
$data["events"][] =
[
'start_date' =>
[
'minute' => $row['min'],
'hour' => $row['hour'],
'month' => $row['mo'],
'day' => $row['day'],
'year' => $row['yr']
],
'text' =>
[
'text' => $row['text']
],
'group' =>
[
'group' => $row['callsign']
]
];
}

echo json_encode($data);

?>

备注:
  • 我选择这种特殊格式是因为它是理解层次结构的最简单方法。通常我不放[在它自己的线上,但在这种情况下它更容易处理。
  • 我选择了[]用于数组定义超过 array()因为它的视觉噪音少得多,因此更容易理解
  • 我用过 $data["events"][] =在循环中,因为我认为它使您定义的数据更加清晰。或者,我可能会做 $events[] =$eventsJson[] =所以变量清楚它应该保存什么信息,然后做 $data=['events'=>$eventsJson];
  • $row[string]不向前兼容。见 https://www.php.net/manual/en/language.types.array.php#language.types.array.foo-bar
  • 关于php - 从 MySQL/PHP 修复 JSON 格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59094146/

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