I want to get the names values from a JSON array.
{
"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"
}
]
}
}
I have tried this following
$responsArray=json_decode($res->getBody());
dd($responsArray->workspaces->workspace->name);
but it return 'trying to access the property of a non-object'. Anyone can help ? I am so appreciated and thank you
$nameArr = [];
$responsArray=json_decode($res->getBody());
foreach($responsArray->workspaces->workspace as $row)
{
$nameArr[] = $row->name;
}
dd($nameArr);
Convert all object levels to multidimensional associative array structure passing true
flag as second argument of json_decode()
function and then access it like accessing to multidimensional array:
$nameArr = [];
$responseArray = json_decode($res->getBody(), true); // set true here
foreach ($responseArray['workspaces']['workspace'] as $row) {
$nameArr[] = $row['name'];
}
dd($nameArr);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.