如何从数组中获得比目标小的值

每当客户取消预订时,我都会尝试向客户退款。由于我根据取消时间有不同的退款百分比,考虑到有人取消预订的时间。


目前主要有两种数据数组:


1) TimeMeta :这是一个保留时隙的数组。


$time_meta = array([

    [

        'id' => '9',

        'start_date' => '2019-06-19',

        'end_date' => '2019-06-19',

        'start_time' => '08:00:00',

        'end_time' => '09:00:00'

    ],

    [

        'id' => '10',

        'start_date' => '2019-06-20',

        'end_date' => '2019-06-20',

        'start_time' => '08:00:00',

        'end_time' => '09:00:00'

    ],

    [

        'id' => '11',

        'start_date' => '2019-06-21',

        'end_date' => '2019-06-20',

        'start_time' => '08:00:00',

        'end_time' => '09:00:00'

    ],

    [

        'id' => '12',

        'start_date' => '2019-06-22',

        'end_date' => '2019-06-20',

        'start_time' => '08:00:00',

        'end_time' => '09:00:00'

    ],

    [

        'id' => '13',

        'start_date' => '2019-06-23',

        'end_date' => '2019-06-23',

        'start_time' => '08:00:00',

        'end_time' => '09:00:00'

    ]

])

2) 按小时返还价值


$refunds = array([

    [

        'before_hours' => '12',

        'refund_percent' => '50'

    ],

    [

        'before_hours' => '24',

        'refund_percent' => '70'

    ],

    [

        'before_hours' => '48',

        'refund_percent' => '90'

    ]

])

到目前为止,我可以计算当前日期时间和保留日期时间之间的差异,如下所示


foreach($time_meta as $ik => $iv){

                $start_datetime = $iv->start_date->format('Y-m-d').' '.$iv->start_time;

                $start_datetime = \Carbon\Carbon::parse($start_datetime);

                $diff = $now->diffInHours($start_datetime,false);


// NEED CODE HERE TO DETERMINE THE EXACT REFUND PERCENT

// ACCORDING TO TIME DIFFERENCE BETWEEN NOW AND THE BOOKED DATE


}

现在,我想找到最匹配的退款百分比。因此,例如,如果用户尝试在 23 小时之前取消预订活动,退款百分比应该仅为 50%,因为 12 小时刚好小于refund数组的23 小时。


此外,如果活动日期已经过去,在这种情况下,$diff价值将为负数,这意味着过去的日期不应退款。


撒科打诨
浏览 181回答 1
1回答

繁星点点滴滴

在 TimeMeta 数组中,它包含子数组,子数组包含多个保留时间数组,这需要嵌套的 foreach 循环。所以 TimeMeta 数组可能如下$time_meta = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => '9',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' => '2019-06-19',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_date' => '2019-06-19',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_time' => '08:00:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_time' => '09:00:00'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => '10',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' => '2019-06-20',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_date' => '2019-06-20',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_time' => '08:00:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_time' => '09:00:00'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => '11',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' => '2019-06-21',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_date' => '2019-06-20',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_time' => '08:00:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_time' => '09:00:00'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => '12',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' => '2019-06-22',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_date' => '2019-06-20',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_time' => '08:00:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_time' => '09:00:00'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => '13',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_date' => '2019-06-23',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_date' => '2019-06-23',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'start_time' => '08:00:00',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'end_time' => '09:00:00'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ];和 Refunds 数组可能如下。$refunds = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'before_hours' => '12',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'refund_percent' => '50'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'before_hours' => '24',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'refund_percent' => '70'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'before_hours' => '48',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'refund_percent' => '90'&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; &nbsp; &nbsp; ];在 TimeMeta 数组中,start_date 和 start_time 格式是正确的日期和时间格式,因此无需转换格式 'Ym-d'。试试下面的代码,foreach($time_meta as $data)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start_datetime = $data['start_date']." ".$data['start_time'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $start_datetime = \Carbon\Carbon::parse($start_datetime);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $diff = \Carbon::now()->diffInHours($start_datetime, false);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // NEED CODE HERE TO DETERMINE THE EXACT REFUND PERCENT&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // ACCORDING TO TIME DIFFERENCE BETWEEN NOW AND THE BOOKED DATE&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $refund_per = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($diff > 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach($refunds as $re)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $hr = $re['before_hours'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($diff < intval($hr))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else if($diff == intval($hr))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $refund_per = $re['refund_percent'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $refund_per = $re['refund_percent'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($refund_per != 0)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "STARTING DATE :".$start_datetime."&nbsp; REFUND PERCENTAGE :".$refund_per ."<BR>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "STARTING DATE :".$start_datetime."&nbsp; NO REFUND <BR>";&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }如果 'before_hours' 值在 Refunds 数组中按升序排序,则此示例代码可以正常工作。
打开App,查看更多内容
随时随地看视频慕课网APP