Php 无法访问空属性

Php 无法访问空属性,php,arrays,object,Php,Arrays,Object,我正在构建这样一个数组: $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'); } 这给了我一个致命的异常“无法访问空属性

我正在构建这样一个数组:

$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');
}
这给了我一个致命的异常“无法访问空属性”


啊,我明白问题所在了

您现在使用它的方式是尝试访问数组索引的元素“total_amount”,而不是数组$list->$index的元素total_amount”。当然,由于$index只是一个数组键,而且是一个字符串或int,所以它不会工作

这个怎么样:

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

$list->$row['categories\u id']=(对象)数组('total\u amount'=>$row['total\u amount'],'other\u stuff'=>'stuff')

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

我不知道您的确切目标,但请尝试以下方法:

$transaction\u list->index['total\u amount']

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


请提供完整的源代码。例如,什么是$rows?谢谢Louis-我改进了我的问题-我需要特别使用$index,因为它是“foreach”循环的一部分。抱歉弄糊涂了啊,我明白问题所在了。您现在使用它的方式是尝试访问数组索引的元素“total_amount”,而不是数组$list->$index的元素total_amount”。当然,由于$index只是一个数组键,而且是一个字符串或int,所以它不会工作。这个怎么样:
是的-行-谢谢!!!!我会接受你的答案-但是你知道通过改变我定义初始数组的方式,是否有可能把它变成$list->$index->total_amount?我想是的-我在上面编辑了我的答案,将两者都包括在内。试试看,然后回答。
<?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