PHP - 查找数组

时间:2017-01-30 16:39:33

标签: php

假设我有一个名为$ selectedDates的数组,其中包含以下内容

$selectedDates = [
    "2000-01-31",
    "2000-02-01",
    "2000-02-02",
    "2000-02-20"
];

PHP中是否有某种方法可以计算出每天连续的#。

所以第一个返回1,第二个返回2,第三个返回3,但第四个将返回1。

我现在正试图遍历它们,但我并没有真正得到任何地方。

<?php foreach ($selectedDates as $date): ?>
    Seriously lost about what to put here.
    <?php echo $consecDayCount; ?>
<?php endforeach; ?>

我正在思考循环内部我可能需要再次循环它?从第一个开始,每天检查一次,每天增加一些增量,表示阵列中的前一个日期是前一天。我只是把头撞在墙上找出那一部分。

3 个答案:

答案 0 :(得分:2)

罗马的一种方式......

CREATE OR REPLACE FUNCTION dynamicJsonValue(varchar(64)) RETURNS VOID AS
'UPDATE "table" SET "field" = ''value''
 WHERE "json_field" @> ''{"key": $1}'';'
 LANGUAGE SQL VOLATILE;

结果:# We assume $selectedDates is sorted at this point. $selectedDates = [ "2000-01-31", "2000-02-01", "2000-02-02", "2000-02-20" ]; # The array we will print. $coll = array(); # We use array_reverse to start from the last entry. foreach(array_reverse($selectedDates) as $date) { $tmp = $date; $con = 1; do $tmp = date('Y-m-d', strtotime("$tmp -1day")); # ++$con is used to count only up if in_array gives true. while(in_array($tmp, $selectedDates) && (++$con)); $coll[$date] = $con; } print_r($coll);

答案 1 :(得分:0)

你走了:

$selectedDates = [
    '2000-01-31',
    '2000-02-01',
    '2000-02-02',
    '2000-02-20',
    '2000-02-21',
];

$format = 'Y-m-d';
$consecutive = [];

$length = count($selectedDates);
$streak = false;
$last = null;

for($index = 0; $index < $length - 1; $index++) {
    $firstDate = DateTime::createFromFormat($format, $selectedDates[$index]);
    $secondDate = DateTime::createFromFormat($format, $selectedDates[$index + 1]);

    $diff = $firstDate->diff($secondDate);

    if ($diff->days === 1) {
        // consecutive dates
        if ($streak) {
            $consecutive[$last]++; // we have another consecutive day to the current streak
        } else {
            $consecutive[$selectedDates[$index]] = 2; // we already have 2 consecutive days
            $last = $selectedDates[$index];
            $streak = true;
        }
    } else {
        $streak = false;
    }
}


var_dump($consecutive);

这将为您提供date => number of consecutive days starting on that date格式的数组。

答案 2 :(得分:0)

另一种方式     

$dates = array(
    new DateTime('2000-01-31'), 
    new DateTime('2000-02-01'), 
    new DateTime('2000-02-02'), 
    new DateTime('2000-02-20'), 

);

// process the array

$lastDate = null;
$ranges = array();
$currentRange = array();

foreach ($dates as $date) {    

    if (null === $lastDate) {
        $currentRange[] = $date;
    } else {

        // get the DateInterval object
        $interval = $date->diff($lastDate);

        // DateInterval has properties for 
        // days, weeks. months etc. You should 
        // implement some more robust conditions here to 
        // make sure all you're not getting false matches
        // for diffs like a month and a day, a year and 
        // a day and so on...

        if ($interval->days === 1) {
            // add this date to the current range
            $currentRange[] = $date;    
        } else {
            // store the old range and start anew
            $ranges[] = $currentRange;
            $currentRange = array($date);
        }
    }

    // end of iteration... 
    // this date is now the last date     
    $lastDate = $date;
}

// messy... 
$ranges[] = $currentRange;

// print dates

foreach ($ranges as $range) {

    // there'll always be one array element, so 
    // shift that off and create a string from the date object 
    $startDate = array_shift($range);
    $str = sprintf('%s', $startDate->format('D j M'));

    // if there are still elements in $range
    // then this is a range. pop off the last 
    // element, do the same as above and concatenate
    if (count($range)) {
        $endDate = array_pop($range);
        $str .= sprintf(' to %s', $endDate->format('D j M'));
    }

    echo "<p>$str</p>";
}  ?>

输出::: 1月31日星期一至2月2日星期三

2月20日星期日