我试图创建一个需要像这样构造的泛型对象:
[Content] => stdClass Object
(
[item] => Array
(
[0] => stdClass Object
(
[Value] => STRING
)
)
[item] => Array
(
[0] => stdClass Object
(
[Value] => ANOTHER STRING
)
)
)这是我的密码:
$content = new stdClass();
$data = file('filname.csv');
foreach($data as $key => $val) {
$content->item->Value = $val;
}这将在每次循环迭代时覆盖自身。通过将item定义为如下数组:
$content->item = array();
...
$content->item[]->Value = $val;...the结果也不是估计值。
发布于 2015-07-30 13:06:24
您每次都要重写数据,甚至使用数组。您应该创建临时对象来存储值,然后将它们放到item数组中。
$content = new \stdClass();
$content->item = array();
foreach($data as $key => $val) {
$itemVal = new \stdClass();
$itemVal->Value = $val;
$content->item[] = $itemVal;
}https://stackoverflow.com/questions/31724473
复制相似问题