4

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

2 Answers 2

4

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...
        }
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you ver much, how can i add a if query like if($success == true){ echo "success"; } ?
Perfect, Thank you very much.
3

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.