Reputation: 111
how can I Select the value of "success" from that json?:
{
"response": {
"success": true,
"groups": [
{
"gid": "3229727"
},
{
"gid": "4408371"
}
]
}
}
Thats my current code:
$result = json_decode ($json);
$success = $result['response'][0]['success'];
echo $success;
Thank you. Regards
Upvotes: 4
Views: 825
Reputation: 41
You are almost near to solution. place "true" as second argument for json_decode()
.
Eg:
$result = json_decode ($json, true);
$result['response']['success'];` -> to get the value of success.
Upvotes: 3
Reputation: 7617
Here You go... with a Quick-Test Here:
<?php
$strJson = '{
"response": {
"success": true,
"groups": [
{
"gid": "3229727"
},
{
"gid": "4408371"
}
]
}
}';
$data = json_decode($strJson);
$success = $data->response->success;
$groups = $data->response->groups;
var_dump($data->response->success); //<== YIELDS:: boolean true
var_dump($groups[0]->gid); //<== YIELDS:: string '3229727' (length=7)
var_dump($groups[1]->gid); //<== YIELDS:: string '4408371' (length=7)
UPDATE:: Handling the value of
success
within a Conditional Block.
<?php
$data = json_decode($strJson);
$success = $data->response->success;
$groups = $data->response->groups;
if($success){
echo "success";
// EXECUTE SOME CODE FOR A SUCCESS SCENARIO...
}else{
echo "failure";
// EXECUTE SOME CODE FOR A FAILURE SCENARIO...
}
Upvotes: 4