简体   繁体   English

PHP 如何根据日期排列数组键值

[英]PHP how to arrange array key values based on date

I want to arrange array key of value based on array value date ( from - to ), for example following is my array and I want to arrange booking date from this array.我想根据数组值日期(从 - 到)排列数组键值,例如以下是我的数组,我想从这个数组中安排预订日期。

Array
(
    [0] => Array
        (
            [id] => 56
            [user_id] => 12
            [name] => A
            [email] => hhjhj@df.com
            [phone] => 4522111111
            [service_name] => a:1:{i:0;s:14:"Beard modeling";}
            [booking_date] => 2021-01-01
            [time] => 18:30:00
            [barbar_id] => 11
            [created_date] => 2021-01-01 12:11:35
        )

    [1] => Array
        (
            [id] => 57
            [user_id] => 12
            [name] => B
            [email] => hhjhj@df.com
            [phone] => 4522111111
            [service_name] => a:1:{i:0;s:14:"Beard modeling";}
            [booking_date] => 2021-01-09
            [time] => 18:30:00
            [barbar_id] => 11
            [created_date] => 2021-01-01 12:11:35
        )

    [2] => Array
        (
            [id] => 58
            [user_id] => 12
            [name] => C
            [email] => hhjhj@df.com
            [phone] => 4522111111
            [service_name] => a:1:{i:0;s:14:"Beard modeling";}
            [booking_date] => 2021-01-14
            [time] => 18:30:00
            [barbar_id] => 11
            [created_date] => 2021-01-01 12:11:35
        )

    [3] => Array
        (
            [id] => 61
            [user_id] => 12
            [name] => GGG
            [email] => d@fdfdf.com
            [phone] => 4522111111
            [service_name] => a:1:{i:0;s:14:"Beard modeling";}
            [booking_date] => 2021-01-17
            [time] => 18:30:00
            [barbar_id] => 11
            [created_date] => 2021-01-01 12:11:35
        )

    [4] => Array
        (
            [id] => 59
            [user_id] => 12
            [name] => k
            [email] => hhjhj@df.com
            [phone] => 4522111111
            [service_name] => a:1:{i:0;s:14:"Beard modeling";}
            [booking_date] => 2021-01-28
            [time] => 18:30:00
            [barbar_id] => 11
            [created_date] => 2021-01-01 12:11:35
        )

)

I want to arrange booking_date like我想安排booking_date

booking_date :  2021-01-01 To 2021-01-09
booking_date :  2021-01-14 To 2021-01-17
booking_date :  2021-01-17 To 2021-01-28

I have tried below the code but it's not considered booking_date: 2021-01-28我已经在代码下面尝试过,但它不被认为是 booking_date: 2021-01-28

foreach ($getUsers as $kt=> $value) {    
    if($kt % 2 ==0){
        echo "booking_date".$getUsers[$kt]['booking_date'].'</br>';  



        $from = $getUsers[$kt]['booking_date'];
    }else{
       echo "booking_date ELSE".$getUsers[$kt]['booking_date'].'</br>';  
      // echo "EEE".$sql.'</br>';  

      $to =$from.' To ' .$getUsers[$kt]['booking_date'];

     }
}

Someone please help me, What I have to change in my script?有人请帮助我,我必须在脚本中更改什么?

Try something like this:尝试这样的事情:

for($i=0;$i<=count($getUsers);$i+2) {  
    if(isset($getUsers[$i]))
        echo "booking_date: ".$getUsers[$i]['booking_date'].'-'.$getUsers[$i+1]['booking_date'].'</br>';  

    }
}

For your original question where you want an output of:对于您想要 output 的原始问题:

booking_date :  2021-01-01 To 2021-01-09
booking_date :  2021-01-14 To 2021-01-17
booking_date :  2021-01-17 To 2021-01-28

I think array_chunk would be convenient, given you are pairing items together to create a 'from' and 'to'.我认为array_chunk会很方便,因为您将项目配对在一起以创建“从”和“到”。

The other part of the solution is just to keep track of the 2nd last booking_date , so we can use it as the from for the last element.解决方案的另一部分只是跟踪倒数第二个booking_date ,因此我们可以将其用作最后一个元素的from

I added in handling for an array with only 1 element as well, as I'm not really sure of your use case.我还添加了处理只有 1 个元素的数组,因为我不太确定您的用例。

 <?php

    $userBookings = [
         0 => [ 'booking_date' => '2021-01-01' ],
         1 => [ 'booking_date' => '2021-01-09' ],
         2 => [ 'booking_date' => '2021-01-14' ],
         3 => [ 'booking_date' => '2021-01-17' ],
         4 => [ 'booking_date' => '2021-01-28' ]
    ];

    $userBookingsPairs = array_chunk($userBookings, 2);

    $fromDate = $toDate = $previousToDate = '';

    foreach ($userBookingsPairs as $userBookingsChunk) {
            $isPair = count($userBookingsChunk) === 2;

            if ($isPair) {
                    $from = reset($userBookingsChunk);
                    $to = end($userBookingsChunk);
                    $fromDate = $from['booking_date'];
                    $previousToDate = $toDate = $to['booking_date'];
            } elseif (!empty($previousToDate)) {
                    // Last element following pairs
                    $to = reset($userBookingsChunk);
                    $fromDate = $previousToDate;
                    $toDate = $to['booking_date'];
            } else {
                    // Single element only
                    $to = reset($userBookingsChunk);
                    $toDate = $to['booking_date'];
                    echo "booking_date: {$toDate}<br>";
                    break;
            }

            echo "booking_date: {$fromDate} To {$toDate}<br>";
    }

As for your second question :至于你的第二个问题

2021-01-01 To 2021-01-09, 2021-01-09 To 2021-01-14, 2021-01-14 To 2021-01-17 And 2021-01-17 To 2021-01-28

So in basic terms, for the first 2 elements, we take the booking_date from element #1 and element #2 and combine them.因此,基本而言,对于前 2 个元素,我们从元素 #1 和元素 #2 中获取booking_date并将它们组合起来。

For the next 2 elements, we combine the booking_date of element #2 with element #3, then element #3 with element #4.对于接下来的 2 个元素,我们将元素 #2 的booking_date与元素 #3 组合,然后将元素 #3 与元素 #4 组合。 So on and so forth until the last element, where we combine element #n-1 with element #n.依此类推,直到最后一个元素,我们将元素#n-1 与元素#n 组合在一起。

 <?php

      $userBookings = [
         0 => [ 'booking_date' => '2021-01-01' ],
         1 => [ 'booking_date' => '2021-01-09' ],
         2 => [ 'booking_date' => '2021-01-14' ],
         3 => [ 'booking_date' => '2021-01-17' ],
         4 => [ 'booking_date' => '2021-01-28' ]
      ];

      $output = '';

      $userBookingsPairs = array_chunk($userBookings, 2);

      $fromDate = $toDate = $previousFromDate = $previousToDate = '';

      foreach ($userBookingsPairs as $userBookingsChunk) {
           $isPair = count($userBookingsChunk) === 2;

           if ($isPair) {
                $from = reset($userBookingsChunk);
                $to = end($userBookingsChunk);
                $fromDate = $from['booking_date'];
                $toDate = $to['booking_date'];
                if (empty($previousFromDate)) {
                     // First iteration
                     $output = "$fromDate To $toDate";
                } else { // 'n'th iteration
                     // print date range using prior element
                     $output .= ", $previousToDate To $fromDate";

                     // print date range for this element
                     $output .= ", $fromDate To $toDate";
                }

                $previousFromDate = $fromDate;
                $previousToDate = $toDate;
           } elseif (!empty($previousToDate)) {
                // Last element following pairs
                $to = reset($userBookingsChunk);
                $output .= ", $previousToDate To {$to['booking_date']}";
           } else {
                // Single element only
                $to = reset($userBookingsChunk);
                $output = $to['booking_date'];
           }
      }

      echo $output;

Use a for bucle, with an increment of 2. Create the interval with "index" - "index+1".使用 a for bucle,增量为 2。使用“index”创建区间 - “index+1”。

For example you can use this code:例如,您可以使用以下代码:

        foreach ($array as $kt => $value) {

        if ($kt % 2 !== 0) {
            continue;
        }

        $to = null;
        if(isset($array[$kt + 1])) {
            $to = ' To ' .$array[$kt + 1]['booking_date'];
        }

        echo 'booking_date: ' . $value['booking_date'] . $to . '<br>';
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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