TanGio
TanGio

Reputation: 766

Filter an array by date

So I have the following array:

Array
(
  [2015-07-10] => 94
  [2015-07-07] => 20
  [2015-07-13] => 6
  [2015-07-09] => 42
  [2015-07-08] => 48
)

This array get data from last 7 days, now I want to filter this array by date and add value = 0 if data not exists. For example if execute this cron today so I need to get data from 07-07-2014 ---- 14-07-2014, for my example I need transform the array like this :

     Array
         (
           [2015-07-07] => 20
           [2015-07-08] => 48
           [2015-07-09] => 42
           [2015-07-10] => 94
           [2015-07-11] => 0
           [2015-07-12] => 0
           [2015-07-13] => 6
           [2015-07-14] => 0 
)

I tried to make an ksort like this : ksort($aFilterdFlashParties) but it did not work. Help me please. Thx in advance. Exist a solution?

Upvotes: 1

Views: 1320

Answers (3)

kamal pal
kamal pal

Reputation: 4207

You can create a custom function for that, see example below: Presently it works only for 7 days from start days, you may customize it more as needed.

$array = Array
(
  '2015-07-10' => 94,
  '2015-07-07' => 20,
  '2015-07-13' => 6,
  '2015-07-09' => 42,
  '2015-07-08' => 48
);

function getExpectedArray($startDate, $dates){
    $endDate = date('Y-m-d', strtotime($startDate . ' +7day'));
    $startDate = date('Y-m-d', strtotime($startDate));
    $nArray = array();
    if($startDate < $endDate){
        while($startDate <= $endDate){
            if(array_key_exists($startDate, $dates)){
                $nArray[$startDate] = $dates[$startDate];
            } else {
                $nArray[$startDate] = 0;
            }
            $startDate = date('Y-m-d', strtotime($startDate . "+1day"));
        }
    }
    return $nArray;
}

echo '<pre>';
print_r(getExpectedArray('2015-07-07', $array));

Output:

Array
(
    [2015-07-07] => 20
    [2015-07-08] => 48
    [2015-07-09] => 42
    [2015-07-10] => 94
    [2015-07-11] => 0
    [2015-07-12] => 0
    [2015-07-13] => 6
    [2015-07-14] => 0
)

Upvotes: 0

splash58
splash58

Reputation: 26153

$input = [
  '2015-07-10' => 94,
  '2015-07-07' => 20,
  '2015-07-13' => 6,
  '2015-07-09' => 42,
  '2015-07-08' => 48
];

// Dates range
$StartDate = '07-07-2015';
$EndDate = '14-07-2015';

// Make array date =>0
$i = strtotime($StartDate);
$iEnd = strtotime($EndDate);
$dates = [];

While ($i <= $iEnd) {
   $dates[date('Y-m-d', $i)] = 0;;
   $i = strtotime('+1day', $i);  
  }
// Set existing data
$res = array_replace($dates, $input);

print_r($res); 

Upvotes: 0

michelem
michelem

Reputation: 14590

Probably something like this could help you:

<?php

 $dateArray = [ 
  '2015-07-10' => 94, 
  '2015-07-07' => 20, 
  '2015-07-13' => 6,
  '2015-07-09' => 42, 
  '2015-07-08' => 48, 
 ];

$begin = new DateTime( '2015-07-07' );
$end = new DateTime( '2015-07-14' );

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

foreach ( $period as $dt ) { 
  $date = $dt->format( "Y-m-d" );
  if (!isset($dateArray[$date])) {
     $dateArray[$date] = 0;
  }   
}

ksort($dateArray);

var_dump($dateArray);
?>

OUTPUT:

{
  ["2015-07-07"]=>
  int(20)
  ["2015-07-08"]=>
  int(48)
  ["2015-07-09"]=>
  int(42)
  ["2015-07-10"]=>
  int(94)
  ["2015-07-11"]=>
  int(0)
  ["2015-07-12"]=>
  int(0)
  ["2015-07-13"]=>
  int(6)
}

Upvotes: 1

Related Questions