Don
Don

Reputation: 89

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 < sizeof($daten) - 1 ; $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 < $i; $j++){
            $tmpArrayDaten[] = $daten[$j];
            $tmpArrayAnzahl[] = $anzahl[$j];
        }

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

        for($j = $i; $j < (sizeof($daten) + 1); $j++){
            $tmpArrayDaten[] = $daten[$j];
            $tmpArrayAnzahl[] = $anzahl[$j];
        }

        $this->logger->lwrite(var_export($tmpArrayDaten));
        $this->logger->lwrite(var_export($tmpArrayAnzahl));

        $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

Upvotes: 1

Views: 51

Answers (1)

Will Hines
Will Hines

Reputation: 334

$data = [2012, 2013, 2016, 2017, 2020];
$anzeige = [1, 5, 8, 3, 10];

list($new_data, $new_clicks) = fillGaps($data, $anzeige);

print_r($new_data);
print_r($new_clicks);

function fillGaps($data, $clicks) {

    // assume the first element is right
    $fixed_data = array($data[0]);
    $fixed_clicks = array($clicks[0]);

    for ($i = 1; $i < count($data); $i++) {

        // if there is a gap, fill it (add 0s to corresponding array)
        while (end($fixed_data)+1 != $data[$i]) {
            $fixed_data[] = end($fixed_data) + 1;
            $fixed_clicks[] = 0;
        }

        // add the data which exists after gap
        $fixed_data[] = $data[$i];
        $fixed_clicks[] = $clicks[$i];
    }   

    return array($fixed_data, $fixed_clicks);
}

Upvotes: 1

Related Questions