无法访问空属性


Cannot access empty property

我正在构建这样的数组:

$result = $this->db->get($this->_table);
foreach ($result->result_array() as $row) 
{
    $list->$row['categories_id'] = array('total_amount' => $row['total_amount'], 'other_stuff' => 'stuff');
}

这给了我一个致命的异常"无法访问空属性"

<?foreach ($category_list as $index => $row) {?>
          <td><?=$row?></td>
          <td><?=$list->$index['total_amount']?></td>
   <?}?>

但这对有效

<?foreach ($category_list as $index => $row) {?>
          <td><?=$row?></td>
          <? $temp = $list->$index; ?>
          <td><?=$temp['total_amount']?></td>
   <?}?>

有没有更好的方法可以从这个对象中获得"总量"?

更棒的是,我如何才能让它像一样运行

 echo $list->$index->total_amount

啊,我看到问题了。

现在使用它的方式是尝试访问数组索引的元素"total_amount",而不是数组$list->$index的元素total_amount'。当然,由于$index只是一个数组键,而字符串或int则不起作用。

这个呢:<td><?=$list->{$index}['total_amount']?></td>

关于您的最后一个问题(通过$list->$index->total_amount访问值):您可能会将数组强制转换为带有(object)的对象。

$list->$row['categories_id'] = (object) array('total_amount' => $row['total_amount'], 'other_stuff' => 'stuff');

-------原始答案-------

我不知道你的确切目标,但试试下面的:

$transaction_list->index['total_amount']

注意到索引前面缺少的$了吗?这是访问类成员变量的实际方式。当使用$时,您实际上在做其他事情。

http://www.php.net/manual/en/language.oop5.basic.php

http://www.php.net/manual/en/language.variables.variable.php