David Yell
David Yell

Reputation: 11855

PHP: Direct access to an element in an array of objects

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

Upvotes: 0

Views: 1033

Answers (2)

Richard JP Le Guen
Richard JP Le Guen

Reputation: 28753

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... ?

Upvotes: 0

Gordon
Gordon

Reputation: 316959

Either

Upvotes: 2

Related Questions