2

I have an array in codeigniter like below. As shown below, there are some spaces between dates. After 1st April, there is 3rd and after that its 10th and so on.

 Array
    (
        [2019-04-01] => 1
        [2019-04-03] => 3
        [2019-04-10] => 8
        [2019-04-15] => 1
    )

I need those empty dates to be there with a value '0' so at last, the array will look like:

    Array
            (
       [2019-04-01] => 1
       [2019-04-02] => 0
       [2019-04-03] => 3
       [2019-04-04] => 0
       [2019-04-05] => 0
       [2019-04-06] => 0
       [2019-04-07] => 0
       [2019-04-08] => 0
       [2019-04-09] => 0
       [2019-04-10] => 8
       [2019-04-11] => 0
       [2019-04-12] => 0
       [2019-04-13] => 0
       [2019-04-14] => 0
       [2019-04-15] => 1
            )

The first and last index of array will work as range.

What I've tried so far

I was using this approach but this doesn't seem to work. In below code, testArr is the array posted above

                $i=0;    
                foreach ($testArr as $key => $value) {

                if($i==count($testArr)-1){break;}

                $currentDay=new DateTime($key);
                $nextDay=new DateTime(array_keys($testArr)[$i]);


                $diff=date_diff($nextDay,$currentDay);
                $diffint=intval($diff->format("%a"));
                if($diffint!==1){
                    $currentDay->modify('+1 day');
                    $temp=array($currentDay->format('Y-m-d')=>0);
                    $testArr = $this->insertArrayAtPosition($testArr,$temp,$i);
                }

                $i++;
            }


If this question already has answers somewhere, then don't forget to mark it as duplicate.

3 Answers 3

5

You can use DatePeriod to generate every date within your target period and fill the values for each from your set.

$values = ['2019-04-01' => 10, '2019-04-02' => 20, '2019-04-04' => 30];

$dates = new DatePeriod(new DateTime('2019-04-01'), new DateInterval('P1D'), new DateTime('2019-04-04'));

foreach($dates as $date) {
    $values[$date->format('Y-m-d')] = $values[$date->format('Y-m-d')] ?? 0;
}

Resulting in:

$values = [
    '2019-04-01' => 10,
    '2019-04-02' => 20,
    '2019-04-03' => 0,
    '2019-04-04' => 30,
]; 
Sign up to request clarification or add additional context in comments.

3 Comments

Its working fine. Its adding the dates but its messing up the order of the dates. The starting 4 index are there which were initially in the array but the filled elements are pushed at the end. Any way to put them on correct position?
@MuhammadOsama YYYY-MM-DD dates are easy to sort, you can use ksort($values).
Thanks Sam :) I appreciate your time
3

You can use DatePeriod to get the range between the first key and the last key of the array.

$arr = array(
    "2019-04-01" => 1,
    "2019-04-03" => 3,
    "2019-04-10" => 8,
    "2019-04-15" => 1,
);

$keys = array_keys( $arr );
$start = new DateTime($keys[0]);
$stop = new DateTime($keys[ count( $keys ) - 1 ]);
$stop->modify('+1 day');

$range = new DatePeriod($start, new DateInterval('P1D'), $stop);
$result = array();

foreach ($range as $key => $value) {
    $date = $value->format('Y-m-d');

    if ( isset( $arr[ $date ] ) ) $result[ $date ] = $arr[ $date ];
    else $result[ $date ] = 0;
}

This will result to:

Array
(
    [2019-04-01] => 1
    [2019-04-02] => 0
    [2019-04-03] => 3
    [2019-04-04] => 0
    [2019-04-05] => 0
    [2019-04-06] => 0
    [2019-04-07] => 0
    [2019-04-08] => 0
    [2019-04-09] => 0
    [2019-04-10] => 8
    [2019-04-11] => 0
    [2019-04-12] => 0
    [2019-04-13] => 0
    [2019-04-14] => 0
    [2019-04-15] => 1
)

3 Comments

Working perfect Eddie ! :) Thanks for helping me out. Appreciated
Just a little question for my personal knowledge, whats the reason for the modification of +1 day? :) You added 1 more day in the range but still its working perfect. Is the last index exclusive from the range?
without the +1 It will end on 2019-04-14. It is range between the start and end so to include the 15, we need to add +1
0

It is possible using DatePeriod please check below code.

<?php
$testArr = array('2019-04-01'=>1,'2019-04-03'=>3,'2019-04-10'=>8,'2019-04-15'=>1);

$start = new DateTime('2019-04-01'); // your start key
$end = new DateTime('2019-04-16'); // your end key

$interval = DateInterval::createFromDateString('1 day');
$period = new DatePeriod($start, $interval, $end);

$finalArr = array();
foreach ($period as $dt) {
    if(array_key_exists($dt->format("Y-m-d"), $testArr)){
        $finalArr[$dt->format("Y-m-d")] = $testArr[$dt->format("Y-m-d")];
    }else{
        $finalArr[$dt->format("Y-m-d")] =  0;   
    }

}
echo '<pre>';
print_r($finalArr);
exit();
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.