PHP:直接访问对象数组中的元素

问题描述:

I am trying to output a set of data, and I have, due to my LEFT OUTER JOIN got multiple rows per match.

I'm using a positive look ahead to see if the next row is the same id as the first. However the return from the database class returns an array of objects.

array(7) {
          [0]=> object(stdClass)#4 (8) {
                                      ["dom_id"]=>string(1) "3"
                                      ["domain"]=>string(11) "example.com"
                                      ["status"]=>string(7) "Invalid"
                                      ["expiry"]=>string(10) "2010-07-20"
                                      ["remaining"]=>string(6) "0 Days"
                                      ["rem_id"]=>NULL
                                      ["alert_type"]=>NULL
                                      ["contact"]=>NULL
  }
//etc

I am getting the following error, Fatal error: Cannot use object of type stdClass as array

My code is as follows,

echo $domains[$k+1]->alert_type;

I know that I could assign the new dimension to a variable and access that as an object, but for the sake of neatness I'd rather access it directly.

Is this possible? ..and if it is, how do I approach it?

Ta

我正在尝试输出一组数据,由于我的 LEFT OUTER JOIN 每次匹配得到多行。 p>

我正在使用正向前看,看看下一行是否与第一行的id相同。 但是,从数据库类返回会返回一个对象数组。 p>

  array(7){
 [0] =>  object(stdClass)#4(8){
 [“dom_id”] => string(1)“3”
 [“domain”] => string(11)“example.com”
 [“  status“] => string(7)”Invalid“
 [”expiry“] => string(10)”2010-07-20“
 [”remaining“] => string(6)”0 天“
 [”rem_id“] => NULL 
 [”alert_type“] => NULL 
 [”contact“] => NULL 
} 
 //等等
  code>  
 
 

我收到以下错误,致命错误:无法使用stdClass类型的对象作为数组 code> p>

我的代码是 如下: p>

  echo $ domains [$ k + 1]  - > alert_type; 
  code>  pre> 
 
 

我知道 我可以将新维度分配给变量并将其作为对象访问,但为了整洁,我宁愿直接访问它。 p>

这是否可能 竹叶提取? ..如果是,我该如何处理? p>

Ta p> div>

Either

Assuming that $domains is your stdClass object which you wish was an array, you could try using variable variables:

echo $domains->{$k+1}->alert_type;

The real problem though is that you have a bunch of stdClass objects. Are you storing these in a session and then reading them later, or are you neglecting to include a file or something... ?