简体   繁体   中英

php prevent foreach in foreach double

I am tryig to get dates between from and to date. By using the datetime class it works fine however when I use it with dynamic result, such as foreach inside foreach the dates is being doubled. I have tried using unique_array but with no luck. Any suggestions? :)

Availabledates content screenshot (ignore the comma):

在此处输入图片说明

Example:

$result = $wpdb->get_results('SELECT fdate, tdate FROM availabledates');
    foreach($result as $data):
        $fdate = date('Y-m-d', $data->fdate);
        $tdate = date('Y-m-d', $data->tdate);

        $begin = new DateTime($fdate);
        $end = new DateTime($tdate);

        $daterange = new DatePeriod($begin, new DateInterval('P1D'), $end);
        foreach($daterange as $date){
            echo $date->format("Y-m-d") . "<br>";
        }
    endforeach;

Link to fiddle

Well. I don't know what to feel -.-. I found out that the range was so big that I thought it was a bug. Turned out it was not. It had a range of 40 days. I should have paid more attention. Thanks for your time: Don't panic :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM