0

How do I get the uname value from this JSON into PHP variable? to use through my page?

    {
        "token": "iutiutiut-0jjjjj0-97987g",
        "auth": {
            "id": 1,
            "app_id": 1,
            "user": {
                "uname": "foo",  
                "role": "member"
            }
    }
}

thanks for some reason just can't get it and I can't find examples similar anywhere on google or I am not calling it correctly.

could you also tell me the correct terminology so I know as well thanks

isuruAb
  • 2,202
  • 5
  • 26
  • 39
Dynamite Media
  • 159
  • 2
  • 10

1 Answers1

1

What you're trying to do is decode JSON. PHP has a built in function for this aptly named... json_decode. Assuming your JSON is a string ($json_string), here is how you would decode it:

$obj = json_decode($json_string);
$uname = $obj->auth->user->uname;

Or, if you prefer the array syntax, use the second argument in json_decode:

$arr = json_decode($json_string, true);
$uname = $obj['auth']['user']['uname'];
Steven Jeffries
  • 3,522
  • 2
  • 20
  • 35
  • thank you i actually see my mistake i had it set up to encode " json_encode($cexecute,JSON_NUMERIC_CHECK | JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT);" and that is what was tossing it off . Thank you – Dynamite Media Feb 14 '17 at 03:27