简体   繁体   English

检查日期是否在 PHP 中的多维数组中的多个日期范围之间

[英]Checking if a date is in between multiple date ranges in a multidimensional array in PHP

I tried my best to solve it but I could not.我尽力解决它,但我不能。 It would be great if you could help me on this.如果你能在这方面帮助我,那就太好了。

I have these two arrays..我有这两个 arrays ..

$firstarray — it contains a date [0] and the product cost [1] on that day. $firstarray — 它包含日期 [0] 和当天的产品成本 [1]。

[0] => Array (
    [0] => 2020-12-01
    [1] => 24.00
    )
[1] => Array (
    [0] => 2020-12-05
    [1] => 16.00
    )
[2] => Array (
    [0] => 2020-12-07
    [1] => 12.00
    )
[3] => Array (
    [0] => 2020-12-15
    [1] => 0.00
    )
[4] => Array (
    [0] => 2020-12-16
    [1] => 0.00
    )
[5] => Array (
    [0] => 2020-12-16
    [1] => 100.00
    )

$secondarray — contains multiple date ranges [0] and [1], where the cost [2] is exceptionally different within the given period. $secondarray — 包含多个日期范围 [0] 和 [1],其中成本 [2] 在给定期间内异常不同。

[0] => Array (
    [0] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-05
        [2] => 42.00
        )
    [1] => Array (
        [0] => 2020-12-06
        [1] => 2020-12-08
        [2] => 35.00
        )
    [2] => Array (
        [0] => 2020-12-09
        [1] => 2020-12-12
        [2] => 76.00
        )
    )
[1] => Array (
    [0] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-05
        [2] => 42.00
        )
    [1] => Array (
        [0] => 2020-12-06
        [1] => 2020-12-08
        [2] => 35.00
        )
    [2] => Array (
        [0] => 2020-12-09
        [1] => 2020-12-12
        [2] => 76.00
        )
    )
[2] => Array (
    [0] => Array (
        [0] => 2020-12-04
        [1] => 2020-12-09
        [2] => 10.00
        )
    [1] => Array (
        [0] => 2020-12-10
        [1] => 2020-12-13
        [2] => 45.00
        )
    )
[3] => Array (
    [0] => Array (
        [0] => 2020-12-04
        [1] => 2020-12-09
        [2] => 10.00
        )
    [1] => Array (
        [0] => 2020-12-10
        [1] => 2020-12-13
        [2] => 45.00
        )
    )
[4] => Array (
    [0] => Array (
        [0] => 2020-12-04
        [1] => 2020-12-09
        [2] => 10.00
        )
    [1] => Array (
        [0] => 2020-12-10
        [1] => 2020-12-13
        [2] => 45.00
        )
    )
[5] => Array (
    [0] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-05
        [2] => 42.00
        )
    [1] => Array (
        [0] => 2020-12-06
        [1] => 2020-12-08
        [2] => 35.00
        )
    [2] => Array (
        [0] => 2020-12-09
        [1] => 2020-12-12
        [2] => 76.00
        )
    )

I want to check if each date in $firstarray is in between one of the date ranges in the $secondarray, if so — echo the value at index [2] from the $secondarray from that date range, if not — echo the value at index [1] from the $firstarray.我想检查 $firstarray 中的每个日期是否在 $secondarray 中的一个日期范围之间,如果是 - 从该日期范围的 $secondarray 中回显索引 [2] 处的值,如果不是 - 回显在$firstarray 中的索引 [1]。

Both arrays will always have the same number, which is 5 in this case (the foreach loop should happen always for that same index only) but the number of the arrays or the date ranges inside could be different.两个 arrays 将始终具有相同的数字,在这种情况下为5 (foreach 循环应始终仅针对相同的索引发生),但 arrays 的数字或内部的日期范围可能不同。

thank you very much!!非常感谢您!!

You can use a foreach loop over the values in $firstarray , setting the default price as the value from that array.您可以对$firstarray中的值使用foreach循环,将默认价格设置为该数组中的值。 You can then use the key from each value to index into $secondarray and compare the date from $firstarray with each of the ranges in $secondarray .然后,您可以使用每个值中的键来索引$secondarray并将$firstarray中的日期与 $ $secondarray中的每个范围进行比较。 If the date is in the range, set the price to the value from that range.如果日期在该范围内,请将价格设置为该范围内的值。

$prices = array();
foreach ($firstarray as $key => $value) {
    $price = $value[1];
    foreach ($secondarray[$key] as $range) {
        if ($value[0] >= $range[0] && $value[0] <= $range[1]) {
            $price = $range[2];
            break;
        }
    }
    $prices[] = $price;
}

print_r($prices);

Output (for your sample data): Output(用于您的示例数据):

Array
(
    [0] => 42
    [1] => 42
    [2] => 10
    [3] => 0
    [4] => 0
    [5] => 100
)

Demo on 3v4l.org 3v4l.org 上的演示

This checks if date of first array is between any of the date-ranges of the second array.这将检查第一个数组的日期是否在第二个数组的任何日期范围之间。 If you want to check other indexes just increment (or decrement) $index.如果要检查其他索引,只需递增(或递减)$index。

To return the correct price just uncomment the要返回正确的价格,只需取消注释

return $firstarray[1]
and
return $secondarray[2]

Output Output

Is between
Is not there
Is not there

Code代码

<?php
/*

================ Firstarray
[0] => Array (
        [0] => 2020-12-01
        [1] => 24.00
    )

================ Secondarray
[0] => Array (
    [0] => Array (
        [0] => 2020-12-01
        [1] => 2020-12-05
        [2] => 42.00
        )
    [1] => Array (
        [0] => 2020-12-06
        [1] => 2020-12-08
        [2] => 35.00
        )
    [2] => Array (
        [0] => 2020-12-09
        [1] => 2020-12-12
        [2] => 76.00
    )
*/

$firstarray = array(array('2020-12-01', '24.00'));
$secondarray = array(array(array('2020-12-01', '2020-12-05', '42.00'), array('2020-12-06', '2020-12-08', '35.00'), array('2020-12-09', '2020-12-12', '76.00')));

$index = 0;
echo check($firstarray[$index], $secondarray[$index]);

function check($firstarray, $secondarray)
{
    foreach ($secondarray as $key => $value) {
        if (
            // Check if date is between two dates
            (date('Y-m-d', strtotime($firstarray[0])) >= date('Y-m-d', strtotime($value[0]))) && 
            (date('Y-m-d', strtotime($firstarray[0])) <= date('Y-m-d', strtotime($value[1])))) 
        {
            echo "Is between".PHP_EOL;
            // return $secondarray[2];
        } else {
            echo "Is not there".PHP_EOL;
        }
    }
    
    // return $firstarray[1];
}

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

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