Abaij
Abaij

Reputation: 873

How to get a variable value in a stdClass object?

I get a returned result from an API in stdClass format. I don't know anything about this type of data. So here it looks like:

object(stdClass)#41 (1) { 
   ["return"]=> 
       object(stdClass)#42 (7) {
          ["afterPayOrderReference"]=> string(32) "d4ab78df6ab2ef84194dd1c1d66240b8"
          ["checksum"]=> string(32) "4f8826a99e9c0a67e578d04b6a625117" 
          ["resultId"]=> int(0) 
          ["statusCode"]=> string(1) "A" 
          ["timestampIn"]=> float(1408533108515) 
          ["timestampOut"]=> float(1408533113616) 
          ["transactionId"]=> int(129525) 
       } 
}

What I need is retrieving the statusCode value. I tried doing like in a post I read:

$array = (array) $stringResult;
$array[0]->statusCode;

But it didn't work. Please, someone explain to me in the simplest way because it's really new to me. Thanks.

Upvotes: 2

Views: 4327

Answers (2)

user969234
user969234

Reputation: 1

Its an object array, so just as you call the array element,

echo $stringResult["return"]->statusCode

Upvotes: -1

AbraCadaver
AbraCadaver

Reputation: 78984

Object properties are accessed with the -> operator. Just do:

echo $stringResult->return->statusCode;

If you wanted an array you would access like this since the array contains an object:

$array = (array)$stringResult;
echo $array['return']->statusCode;

Upvotes: 2

Related Questions