PHP: Creating default object from empty value
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?
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);
链接地址: http://www.djcxy.com/p/59396.html
上一篇: 在PHP $ array [] = $ value或array中使用哪个更好
下一篇: PHP:从空值创建默认对象