如何使用 PHP while 语句构建多维 json 数组?
How to build a multidimensional json array with PHP while statement?
正如您在下面的代码中看到的,2020-01-13 上有两个约会,但它们是作为单独的数组生成的。因为它们在同一天,我怎样才能把它们放在同一个阵列上?我正在使用 mysqli 从数据库中获取所有信息,但为了这段代码,我只打印必要的信息。它有效,但我需要知道如何在同一数组中收集具有相同日期的约会。
php
//declare variable
$result = array();
//iterate
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
$result[] = array(
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
);
}
//send response back to jquery
header('Content-Type: application/json');
echo json_encode($result);
这个 php 打印这个
[{"2020-01-15":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Jen Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"John Doe",
"status":"Booked",
"time":"2:00 pm - 5:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Alfred Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
}
]
但我想要的结果是这样
[{
"2020-01-15": {
"number": "1",
"badgeClass": "",
"url": "url",
"dayEvents": {
"title": "Jen Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
}
},
{
"2020-01-13": {
"number": "2",
"badgeClass": "",
"url": "url",
"dayEvents": [{
"title": "John Doe",
"status": "Booked",
"time": "2:00 pm - 5:00 pm"
},
{
"title": "Alfred Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
]
}
}
]
这段代码在创建日程表时,会创建由日期本身索引的每一天事件,然后当发现新事件时,它将检查是否已设置此日期。如果是这样,它将增加计数并添加新事件。唯一复杂的是它需要检查它是否只有 1 个现有条目——这需要先转换为数组。要删除索引,json_encode()
首先使用 array_values()
...
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
if ( isset($result[$schedule_date])){
// Increment count
$result[$schedule_date][$schedule_date]["number"]++;
// Convert single item to array
if ( !isset($result[$schedule_date][$schedule_date]["dayEvents"][0])) {
$result[$schedule_date][$schedule_date]["dayEvents"] =
[$result[$schedule_date][$schedule_date]["dayEvents"]];
}
// Add in new event to dayEvents
$result[$schedule_date][$schedule_date]["dayEvents"][] = [
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
];
}
else {
// Create new days schedule
$result[$schedule_date] = [
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
];
}
}
header('Content-Type: application/json');
echo json_encode(array_values($result), JSON_PRETTY_PRINT);
根据我对上面提供的代码和所需输出的观察,您可以这样做:
$scheduledDate = date('Y-m-d');
$my_schedule_start = "";
$my_schedule_end = "";
$finalResult = [];
while($schedule = $result_data->fetch_assoc()){
$start_time =date("g:i a", strtotime($schedule['start_time']));
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
$res = new stdClass;
$res->number = 1;
$res->badgeClass = "";
$res->url = 'url';
$dayEvents = new stdClass;
$dayEvents->title = $schedule['first_name'].' '.$schedule['last_name'];
$dayEvents->status = $schedule['cleaning_status'];
$dayEvents->time = $start_time .' - '.$end_time;
$res->dayEvents = $dayEvents;
$finalResult[] = $res;
}
echo json_encode($finalResult);
这里需要注意的事项:
1.) 我已经根据提供的输出中的第一个条目给出了代码库。如果你想更深入的话,而不是在$dayEvents
上使用stdClass
,你可以直接将$dayEvents
声明为一个数组并在里面使用单独的stdClass
并将它推到dayEvents
就像我为 $finalResult
数组所做的那样。
2.) 您可能会收到 $my_schedule_start
和 $my_schedule_end
之前未声明的通知,因此我冒昧地在 While 循环之外用空值声明了它们。但请随时根据您的需要更改它们。
3.) 如果 $res = new stdClass;
不起作用,那么您可以尝试 $res = new \stdClass
。
4.) 如果您发现任何语法错误,请原谅我,因为我也是人类:)
总结:
stdClass 是 PHP 的内部功能,用于在 PHP 中声明对象。您还可以使用 $dayEvents = {};
之类的短语法代替 $dayEvents = new stdClass;
.
您可以在 stdClass 上找到更多信息 Here
希望这对您有所帮助。
正如您在下面的代码中看到的,2020-01-13 上有两个约会,但它们是作为单独的数组生成的。因为它们在同一天,我怎样才能把它们放在同一个阵列上?我正在使用 mysqli 从数据库中获取所有信息,但为了这段代码,我只打印必要的信息。它有效,但我需要知道如何在同一数组中收集具有相同日期的约会。
php
//declare variable
$result = array();
//iterate
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
$result[] = array(
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
);
}
//send response back to jquery
header('Content-Type: application/json');
echo json_encode($result);
这个 php 打印这个
[{"2020-01-15":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Jen Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"John Doe",
"status":"Booked",
"time":"2:00 pm - 5:00 pm"}
}
},
{"2020-01-13":{
"number":"1",
"badgeClass":"",
"url":"url",
"dayEvents":{
"title":"Alfred Doe",
"status":"Booked",
"time":"11:00 am - 12:00 pm"}
}
}
]
但我想要的结果是这样
[{
"2020-01-15": {
"number": "1",
"badgeClass": "",
"url": "url",
"dayEvents": {
"title": "Jen Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
}
},
{
"2020-01-13": {
"number": "2",
"badgeClass": "",
"url": "url",
"dayEvents": [{
"title": "John Doe",
"status": "Booked",
"time": "2:00 pm - 5:00 pm"
},
{
"title": "Alfred Doe",
"status": "Booked",
"time": "11:00 am - 12:00 pm"
}
]
}
}
]
这段代码在创建日程表时,会创建由日期本身索引的每一天事件,然后当发现新事件时,它将检查是否已设置此日期。如果是这样,它将增加计数并添加新事件。唯一复杂的是它需要检查它是否只有 1 个现有条目——这需要先转换为数组。要删除索引,json_encode()
首先使用 array_values()
...
while($schedule = $result_data->fetch_assoc()){
//start time conversion into american time
// 24-hour time to 12-hour time
$start_time =date("g:i a", strtotime($schedule['start_time']));
//end time conversion into american time
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
if ( isset($result[$schedule_date])){
// Increment count
$result[$schedule_date][$schedule_date]["number"]++;
// Convert single item to array
if ( !isset($result[$schedule_date][$schedule_date]["dayEvents"][0])) {
$result[$schedule_date][$schedule_date]["dayEvents"] =
[$result[$schedule_date][$schedule_date]["dayEvents"]];
}
// Add in new event to dayEvents
$result[$schedule_date][$schedule_date]["dayEvents"][] = [
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
];
}
else {
// Create new days schedule
$result[$schedule_date] = [
"$schedule_date" => [
"number"=>'1',
"badgeClass"=>'',
"url"=>'url',
"dayEvents"=>[
"title"=>$schedule['first_name'].' '.$schedule['last_name'],
"status"=>$schedule['cleaning_status'],
"time"=>$start_time .' - '.$end_time
]
]
];
}
}
header('Content-Type: application/json');
echo json_encode(array_values($result), JSON_PRETTY_PRINT);
根据我对上面提供的代码和所需输出的观察,您可以这样做:
$scheduledDate = date('Y-m-d');
$my_schedule_start = "";
$my_schedule_end = "";
$finalResult = [];
while($schedule = $result_data->fetch_assoc()){
$start_time =date("g:i a", strtotime($schedule['start_time']));
$end_time =date("g:i a", strtotime($schedule['end_time']));
$my_schedule_start .= $start_time;
$my_schedule_end .= $end_time;
$schedule_date= $schedule['cleaning_date'];
$res = new stdClass;
$res->number = 1;
$res->badgeClass = "";
$res->url = 'url';
$dayEvents = new stdClass;
$dayEvents->title = $schedule['first_name'].' '.$schedule['last_name'];
$dayEvents->status = $schedule['cleaning_status'];
$dayEvents->time = $start_time .' - '.$end_time;
$res->dayEvents = $dayEvents;
$finalResult[] = $res;
}
echo json_encode($finalResult);
这里需要注意的事项:
1.) 我已经根据提供的输出中的第一个条目给出了代码库。如果你想更深入的话,而不是在$dayEvents
上使用stdClass
,你可以直接将$dayEvents
声明为一个数组并在里面使用单独的stdClass
并将它推到dayEvents
就像我为 $finalResult
数组所做的那样。
2.) 您可能会收到 $my_schedule_start
和 $my_schedule_end
之前未声明的通知,因此我冒昧地在 While 循环之外用空值声明了它们。但请随时根据您的需要更改它们。
3.) 如果 $res = new stdClass;
不起作用,那么您可以尝试 $res = new \stdClass
。
4.) 如果您发现任何语法错误,请原谅我,因为我也是人类:)
总结:
stdClass 是 PHP 的内部功能,用于在 PHP 中声明对象。您还可以使用 $dayEvents = {};
之类的短语法代替 $dayEvents = new stdClass;
.
您可以在 stdClass 上找到更多信息 Here
希望这对您有所帮助。