// The predefined dates
$days = array(
"day1" => $day1,
"day2" => $day2,
"day3" => $day3,
"day4" => $day4,
"day5" => $day5,
"day6" => $day6,
"day7" => $day7,
);
// The result set
$forecasts = $this->getForecasts($id, $days);
// Compare dates
$d1 = false;
$d2 = false;
$d3 = false;
$d4 = false;
$d5 = false;
$d6 = false;
$d7 = false;
foreach ($forecasts as $forecast) {
if ($days["day1"] === $forecast->date && !$d1) {
$d1 = true;
}
if ($days["day2"] === $forecast->date && !$d2) {
$d2 = true;
}
if ($days["day3"] === $forecast->date && !$d3) {
$d3 = true;
}
if ($days["day4"] === $forecast->date && !$d4) {
$d4 = true;
}
if ($days["day5"] === $forecast->date && !$d5) {
$d5 = true;
}
if ($days["day6"] === $forecast->date && !$d6) {
$d6 = true;
}
if ($days["day7"] === $forecast->date && !$d7) {
$d7 = true;
}
}
if (!$d1) {
$this->array_put_to_position($forecasts, $days[“day1”], 0, ‘date’);
}
if (!$d2) {
$this->array_put_to_position($forecasts, $days[“day2”], 1, ‘date’);
}
if (!$d3) {
$this->array_put_to_position($forecasts, $days[“day3”], 2, ‘date’); // This is where it goes wrong
}
if (!$d4) {
$this->array_put_to_position($forecasts, $days[“day4”], 3, ‘date’);
}
if (!$d5) {
$this->array_put_to_position($forecasts, $days[“day5”], 4, ‘date’);
}
if (!$d6) {
$this->array_put_to_position($forecasts, $days[“day6”], 5, ‘date’);
}
if (!$d7) {
$this->array_put_to_position($forecasts, $days[“day7”], 6, ‘date’);
}
public function array_put_to_position(&$array, $object, $position, $name = null)
{
$count = 0;
$return = array();
foreach ($array as $k => $v)
{
// insert new object
if ($count == $position)
{
if (!$name) $name = $count;
$return[$name] = $object;
$inserted = true;
}
// insert old object
$return[$k] = $v;
$count++;
}
if (!$name) $name = $count;
if (!$inserted) $return[$name];
$array = $return;
return $array;
}