-1

正如您在下面的代码中看到的,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"
                }
            ]
        }
    }
]
4

2 回答 2

1

此代码在创建计划时创建由日期本身索引的每一天事件,然后当发现新事件时,它将检查该日期是否已设置。如果是这样,它将增加计数并添加新事件。唯一的复杂之处是它需要检查它是否只有 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);
于 2020-01-17T09:41:11.857 回答
0

根据我对上面提供的代码和所需输出的观察,您可以执行以下操作:

$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.) 我已经根据提供的输出中的第一个条目给出了代码库。如果你想更深入,而不是使用stdClasson $dayEvents,你可以直接声明$dayEvents为一个数组并stdClass在里面使用 individual 并将它推送到就像我为数组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 的更多信息

希望这可以帮助。

于 2020-01-17T09:44:46.317 回答