如何从 php 中的 json_decode 获取 JSON 值

How do you get a JSON value from json_decode in php

我正在尝试访问 JSON 数组的图像字符串以获取徽标和图标。

这是我得到的,但它不起作用。


$FinalResponseImages = $responseImages->getBody();

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

我试过了


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

但还是没有。

下面是json_decode()的关联数组

    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
    }
  }
}

如有任何帮助,我们将不胜感激。

您错过了“响应”数组位。例如。 $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