使用 Laravel 5.7 :如何从数组中获取 json 值名称?

using Laravel 5.7 : how can i get json value name from an array ?

我想从 JSON 数组中获取名称值。

{
"workspaces": {
    "workspace": [
        {
            "name": "SITR",
            "href": "http://localhost:8080/geoserver/rest/workspaces/SITR.json"
        },
        {
            "name": "fire",
            "href": "http://localhost:8080/geoserver/rest/workspaces/fire.json"
        },
        {
            "name": "info-geospasial",
            "href": "http://localhost:8080/geoserver/rest/workspaces/info-geospasial.json"
        },
        {
            "name": "pertanian",
            "href": "http://localhost:8080/geoserver/rest/workspaces/pertanian.json"
        },
        {
            "name": "semangat21",
            "href": "http://localhost:8080/geoserver/rest/workspaces/semangat21.json"
        },
        {
            "name": "semangat",
            "href": "http://localhost:8080/geoserver/rest/workspaces/semangat.json"
        },
        {
            "name": "cobalagi",
            "href": "http://localhost:8080/geoserver/rest/workspaces/cobalagi.json"
        },
        {
            "name": "cobak",
            "href": "http://localhost:8080/geoserver/rest/workspaces/cobak.json"
        }
    ]
}

}

我试过以下

$responsArray=json_decode($res->getBody());
dd($responsArray->workspaces->workspace->name);

但它 return 'trying to access the property of a non-object'。 任何人都可以帮忙吗? 我很感激也谢谢你

$nameArr = [];
$responsArray=json_decode($res->getBody());
foreach($responsArray->workspaces->workspace as $row)
{
  $nameArr[] = $row->name;
}
dd($nameArr);

将所有对象级别转换为多维关联数组结构,将 true 标志作为 json_decode() 函数的第二个参数传递,然后像访问多维数组一样访问它:

$nameArr = [];
$responseArray = json_decode($res->getBody(), true); // set true here
foreach ($responseArray['workspaces']['workspace'] as $row) {
    $nameArr[] = $row['name'];
}
dd($nameArr);