codeigniter试图获取非对象的属性

时间:2015-11-25 08:29:24

标签: codeigniter

$ account_types = $ this-> members_model-> getAllAccountTypes();

$result_set = array();
foreach($account_types as $account_type){
    $result_set[$account_type->db_name] = array(
        'subscribed'   => $this->members_model->getAllSubscribedMembers($account_type->id),
        'unsubscribed' => $this->members_model->getAllUnsubscribedMembers($account_type->id)
    );
}

echo json_encode($ result_set);

返回;

我有这些代码行返回我试图在foreach上获取非对象的属性($ account_types为$ account_type)

2 个答案:

答案 0 :(得分:0)

更改您的代码,它将解决您的问题: -

$result_set = array();
$account_types = json_decode(json_encode($account_types), true);
foreach($account_types as $account_type){
    $result_set[$account_type->db_name] = array(
        'subscribed'   => $this->members_model->getAllSubscribedMembers($account_type->id),
        'unsubscribed' => $this->members_model->getAllUnsubscribedMembers($account_type->id)
    );
}

答案 1 :(得分:0)

始终在模型中处理您的查询结果

这可能是查询返回null,因此您最好先测试结果:

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->title;
      echo $row->name;
      echo $row->body;
   }
}

您还可以将字符串传递给result(),它表示要为每个结果对象实例化的类。 More Information