0

I am having a problem with this code:

$days = array();
$d = new StdClass;
for($i = 0; $i < 7; $i++){
    $day = date("Y-m-d", strtotime("-".$i." day"));
    $d->x = $day; //error
    $days[] = $d;
    unset($d);
}
dd($days);

Even thought I have declared a new object it shows me error:

Creating default object from empty value.

How could i possibly resolve this problem?

Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42
Ethris
  • 146
  • 2
  • 13

1 Answers1

1

Try this, Hope this will help you out. You should define $d = new StdClass; with in the loop. For initiating a new object everytime.

Try this code snippet here

<?php

ini_set('display_errors', 1);
$days = array();
for ($i = 0; $i < 7; $i++)
{
    $d = new StdClass;
    $day = date("Y-m-d", strtotime("-" . $i . " day"));
    $d->x = $day; //error
    $days[] = $d;
    unset($d);
}
print_r($days);
Sahil Gulati
  • 15,028
  • 4
  • 24
  • 42