对象php中的多个值


multiple value in object php

对不起,我在对象和方面不是很好

我想做这样的事情:

$this->countries       =new stdclass;
$this->countries->id   =array();
$this->countries->name =array();
$query = $db
    ->getQuery(true)
    ->select('name,id')
    ->from('#__country');
$rows = $db
    ->setQuery($query)
    ->loadObjectList();
foreach ($rows as $row) {
    $this->countries->id  = $row->id;
    $this->countries->name  = $row->name;
}
foreach ($this->countries as $country):
   echo $country->id;
   echo $country->name;
endforeach;

我收到以下错误:注意:正在尝试获取非对象的属性我不知道为什么它不起作用

感谢

对于foreach($this->countries为$country):代码要正确工作,则$this->countries必须是一个数组。建议对代码进行更改。。。(未经测试)

$this->countries       = array();
//    $this->countries       = new stdclass;
//    $this->countries->id   =array();
//    $this->countries->name =array();
$query = $db
    ->getQuery(true)
    ->select('name,id')
    ->from('#__country');
$rows = $db
    ->setQuery($query)
    ->loadObjectList();
foreach ($rows as $row) {
    $country      = new stdclass;
    $country->id  = $row->id;
    $country->name  = $row->name;
    $this->countries[] = $country;   
}
foreach ($this->countries as $country) {
   echo $country->id;
   echo $country->name;
}