[英]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);
但它返回“嘗試訪問非對象的屬性”。 有人可以幫忙嗎? 非常感謝,謝謝
$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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.