Search code examples
phpapijsondecoder

How do you get a JSON value from json_decode in php


I am trying to access the image string of the JSON array for the logo and icon.

Here is what I've got but it's not working.


$FinalResponseImages = $responseImages->getBody();

$thingImages = json_decode($FinalResponseImages,true);
            
$logo = $thingImages["logo"]['image'];
            
$icon = $thingImages['icon']['image'];

Ive tried


$logo = $thingImages{'logo'}->image;
            
$icon = $thingImages{'icon'}->image;

but still nothing.

The associative array of the json_decode() is this below

    array(2) {
  ["statusCode"]=>
  int(200)
  ["response"]=>
  array(2) {
    ["logo"]=>
    array(3) {
      ["safe"]=>
      bool(true)
      ["image"]=>
      string(48) "https://assets.brandfetch.io/7fb7161ad320475.png"
      ["svg"]=>
      NULL
    }
    ["icon"]=>
    array(2) {
      ["image"]=>
      string(48) "https://assets.brandfetch.io/a407773817604b9.png"
      ["svg"]=>
      NULL
    }
  }
}

Any help would be appreciated.


Solution

  • You have missed the "response" array bit. E.g. $thingImages['response']['logo']['image'].

    <?php
    
    $thingImages = [
        'statusCode' => 200,
        'response' => [
            'logo' => [
                'safe'  => true,
                'image' => 'https://assets.brandfetch.io/7fb7161ad320475.png',
                'svg'   => null,
            ],
            'icon' => [
                'image' => 'https://assets.brandfetch.io/a407773817604b9.png',
                'svg'   => null,
            ],
        ],
    ];
    
    $logo = $thingImages['response']['logo']['image'];
    $icon = $thingImages['response']['icon']['image'];
    
    var_dump($logo); // https://assets.brandfetch.io/7fb7161ad320475.png
    var_dump($icon); // https://assets.brandfetch.io/a407773817604b9.png