0

I have a function in php that returns this json array, this is the function

    $token = $db->getTokenFromEmail($email);
    echo $token;

and this is what i get:

[{"unique_id":"cBuJ-xsDDAo:APA91bHYgPwuwXGVxNMuW_Xs0u5bvbr_QSJq8G1_tZ-nGHOdRB0Nv5ijb2BcaP_wUkpyxwERo7cuQxj89YHjOZdIeIwBOGyeHMP_Ywkg_mocfZQr-CxOzy41i8GKj3X6WFjLZJU4ZcbK"}]

My question is how can I get the value (cBuJ-xsD...)? I have tried this but it doesn't work

$obj = json_decode($token,true);
    echo $obj['unique_id'];
4
  • its another dimension inside, print_r it, and you'll see the structure Commented Dec 2, 2016 at 1:28
  • What do you mean? Can you explain me ? (I'm sorry, I'm a newbie in this subject ) @Ghost Commented Dec 2, 2016 at 1:31
  • the answer's below, check it out Commented Dec 2, 2016 at 1:33
  • @Ghost a vote will be appreciated :) thanks in advance! Commented Dec 2, 2016 at 1:41

1 Answer 1

1

Your token's type is a string, and you are correct to use json_decode.

If you try to var_dump the value you will get:

$token = '[{"unique_id":"cBuJ-xsDDAo:APA91bHYgPwuwXGVxNMuW_Xs0u5bvbr_QSJq8G1_tZ-nGHOdRB0Nv5ijb2BcaP_wUkpyxwERo7cuQxj89YHjOZdIeIwBOGyeHMP_Ywkg_mocfZQr-CxOzy41i8GKj3X6WFjLZJU4ZcbK"}]';
$obj = json_decode($token,true);
var_dump($obj);

And the output is:

array(1) {
  [0]=>
  array(1) {
    ["unique_id"]=>
    string(152) "cBuJ-xsDDAo:APA91bHYgPwuwXGVxNMuW_Xs0u5bvbr_QSJq8G1_tZ-nGHOdRB0Nv5ijb2BcaP_wUkpyxwERo7cuQxj89YHjOZdIeIwBOGyeHMP_Ywkg_mocfZQr-CxOzy41i8GKj3X6WFjLZJU4ZcbK"
  }
}

You can see that the $obj variable is an array, and it's first element is another array that has the unique_id key.

To get to that you should use:

$obj[0]['unique_id'];
Sign up to request clarification or add additional context in comments.

1 Comment

I'm so grateful! Just what I needed. Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.