0

I have the following code:

$response = $client->get_json($query);
var_dump($response);

which shows me this in a browser:

object(stdClass)#8 (7) {
  ["search_metadata"]=>
  object(stdClass)#7 (8) {
    ["id"]=>
    string(24) "5fa06b463ffd1f75b14c1b98"
    ["status"]=>
    
    ...
    
  }
  ["search_parameters"]=>
  object(stdClass)#9 (9) {
    ["engine"]=>
    string(6) "google"
    ["q"]=>
    
    ...
    
  }
  ["inline_images"]=>
  array(10) {
    [0]=>
    object(stdClass)#11 (2) {
      ["link"]=>
      string(231) "/search?q=796714619071&num=30&gl=us&hl=en&tbm=isch&source=iu&ictx=1&fir=iYsIHfZTBqNwZM%252Cfl2S346slZj2tM%252C_&vet=1&usg=AI4_-kRkH4vMP2OKxQ8Mz6SJlNoImL7gPg&sa=X&ved=2ahUKEwiCzK_n2OTsAhUKWa0KHQauCKkQ9QF6BAgHEAY#imgrc=iYsIHfZTBqNwZM"
      ["thumbnail"]=>
      
    ....

I'm trying to access inline_images with the following PHP code:

$inlineImages = $response['inline_images'];

But whenever I add this line I'm getting a HTTP 500 error. What am I doing wrong? Why can't I see to access inline_images? Commenting the line out makes the page appear properly.

Ethan Allen
  • 14,425
  • 24
  • 101
  • 194

1 Answers1

1

It's a property on an object, so:

$inlineImages = $response->inline_images;

That's an array of objects, so you can loop through them like this:

foreach ($inlineImages as $inlineImage) {
  $link = $inlineImage->link;
}
rjdown
  • 9,162
  • 3
  • 32
  • 45
  • Ahhh is see. I needed them to be arrays so I just did `$array = json_decode(json_encode($response), true);` to get my desired syntax working. The object was coming from a third-party library. – Ethan Allen Nov 02 '20 at 20:53