Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

SOLVED: Add Date/Mont/Year if is missing to array


I want to have an Array which is always ascending without a gap between days, months or years. So, at the end I want to have something like this for example 2012, 2013, 2014, 2015, 2016.

Example arrays:
$data = [2012, 2013, 2015, 2017]
$anzeige = [1, 5, 8, 3]

Want I want to have at the end:
$data = [2012, 2013, 2014, 2015, 2016, 2017]
$anzeige = [1, 5, 0, 8, 0, 3]

But sometimes there are no data avaiveble for some days, months or years. So, I have a gap. I want to close this gap and add the following day, month or year to the array and it should remain ascending.

This is my code so far:

private function fixDates($daten, $anzahl){
$tmpArrayDaten = array();
$tmpArrayAnzahl = array();

$this->logger->lwrite("Fix Data");

for ($i=0; $i if(($i + 1) == sizeof($daten)){
return array(array("daten" => $daten), array("anzahl" => $anzahl));

if(!(($daten[$i] + 1) == ($daten[$i + 1]))){

$this->logger->lwrite(($daten[$i] + 1) . " != " . ($daten[$i + 1]));

for($j = 0; $j $tmpArrayDaten[] = $daten[$j];
$tmpArrayAnzahl[] = $anzahl[$j];

$tmpArrayDaten[] = $daten[$i] + 1;
$tmpArrayAnzahl[] = 0;

for($j = $i; $j $tmpArrayDaten[] = $daten[$j];
$tmpArrayAnzahl[] = $anzahl[$j];


$this->fixDates($tmpArrayDaten, $tmpArrayAnzahl);

return array(array("daten" => $daten), array("anzahl" => $anzahl));


The $anzahl array contains the value for the day, month or year at the same index in the $daten array. And no, I do not want to have them in one array because I am going to create one array at the end to send it via json_encode() to JavaScript.

However, I cannot find my failure in my code... It never stops. It doesn't stop to add "placeholder" if there is a gap...

Do you may have an idea how to fix that?

Kind regards

Posted in S.E.F
via StackOverflow & StackExchange Atomic Web Robots
This Question have been answered

This post first appeared on Stack Solved, please read the originial post: here

Share the post

SOLVED: Add Date/Mont/Year if is missing to array


Subscribe to Stack Solved

Get updates delivered right to your inbox!

Thank you for your subscription