-2

I know this might have been asked a number of times. but in my case i get this error by using this

echo $result["tracks"]["title"];

Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\withheader.php on line 26

and by using print_r ($result); I get the following result

I want to get the result of array tracks and the element title. so ideally the output should be a variable with value DHL (Deprecated)

stdClass Object ( [increment_id] => 100000045 [store_id] => 1 [created_at] => 2015-09-07 07:34:06 [updated_at] => 2015-09-18 06:02:02 [shipping_address_id] => 441 [order_id] => 224 [total_qty] => 2.0000 [shipment_id] => 48 [items] => Array ( [0] => stdClass Object ( [parent_id] => 48 [sku] => testsimpleadmin [name] => testsimpleadmin [order_item_id] => 642 [product_id] => 57050 [weight] => 2.0000 [price] => 100.0000 [qty] => 1.0000 [item_id] => 96 ) [1] => stdClass Object ( [parent_id] => 48 [sku] => 12121221 [name] => women fancy bag [order_item_id] => 643 [product_id] => 57090 [weight] => 1.0000 [price] => 71.0000 [qty] => 1.0000 [item_id] => 97 ) ) [tracks] => Array ( [0] => stdClass Object ( [parent_id] => 48 [created_at] => 2015-09-18 06:02:02 [updated_at] => 2015-09-18 06:02:02 [carrier_code] => dhl [title] => DHL (Deprecated) [number] => 456 [order_id] => 224 [track_id] => 5 ) ) [comments] => Array ( ) ) 
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54
CMS Test
  • 23
  • 5

2 Answers2

1

Your problem is that $result is not an array but an Object.

So you have to access $result like you would do with a normal object with ->. From your print_r we can see that the attribute tracksis an array with just one element in it. So by doing $result->tracks[0]->title should work

DevAlien
  • 2,456
  • 15
  • 17
1

As seen from your print_r($result) its an object and not an array. So instead of calling it like as

echo $result["tracks"]["title"];

you need to use

echo $result->tracks[0]->title
Narendrasingh Sisodia
  • 21,247
  • 6
  • 47
  • 54