skip to Main Content

I have this JSON response from a controller:

{"success":1,"message":"Transaction assigned to customer."}

How do I get the success key to print 1? I have tried

$json = json_decode($assign);
$success = $json->success;

But getting error: Trying to access array offset on value of type null

2

Answers


  1. Chosen as BEST ANSWER

    I have managed to get it working:

    $response = $data = {"success":1,"message":"Transaction assigned to customer."}; 
    $data = collect($response);
    
    $success = $data["original"]["success"];
    

  2. in your case as you explained in the comments:
    use this one

    first decode the json as object,

    $data = '{"success":1,"message":"Transaction assigned to customer."}';
    $json = json_decode($data, false);
    

    this lets you to access your value using $json->success

    note: the second parameter of json_decode is boolean and named associative which is true by default.

    and do your logic…

    in the Log part, Log::info has two parameter first one is message and expected to be a string.

    and second one is context and expected to be an array. just change your $json into an associative array and send it to Log::info like this:

    Log::info("some message", (array)$json);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search