我希望有一个数组,它总是在几天,几个月或几年之间没有差距的情况下提升。所以,最后我希望有类似的东西,例如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]
但有时几天,几个月或几年都没有数据可用。所以,我有一个差距。我想缩小这个差距并将第二天,一个月或一年添加到数组中,它应该保持升序。
到目前为止,这是我的代码:
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));
}
$anzahl
数组包含$daten
数组中相同索引处的日,月或年的值。不,我不想让它们在一个数组中,因为我将在最后创建一个数组,通过json_encode()
将它发送到JavaScript。
然而,我在代码中找不到我的失败......它永远不会停止。它并没有停止添加&#34;占位符&#34;如果有差距...
你可能知道如何解决这个问题吗?
亲切的问候
答案 0 :(得分:1)
$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);
}