0

I'm running a CURL in PHP and if I var_dump the response, I get the following and I'm saving as $response:

{
    "name": "Bob Smithy",
    "email": "testing@gmail.com",
    "email_verified": false,
    "connection": "Username-Password-Authentication",
    "user_id": "f8d305bb49a3",
    "provider": "auth0",
    "picture": "https://secure.gravatar.com/avatar/a2da375af0228d88aed6b723e6764a94?s=480&r=pg&d=https%3A%2F%2Fcdn.auth0.com%2Favatars%2Fbs.png",
    "nickname": "Bob Smithy",
    "identities": [
        {
            "connection": "Username-Password-Authentication",
            "user_id": "f8d305bb49a3",
            "provider": "auth0",
            "isSocial": false
        }
    ],
    "updated_at": "2015-08-05T21:11:56.202Z",
    "created_at": "2015-08-05T21:11:56.202Z"
}

What I need to do is get the value for user_id that's under identities. I know I need to do json_decode, but then I can't figure out how to structure the rest. I know this doesn't work:

$dec_respo = (json_decode($response, true));    
$new_data = $deco_respo['identities']['user_id'];

What should I be doing here to get this value?

jonmrich
  • 4,233
  • 5
  • 42
  • 94
  • You need to *json_ **de** code()* it (But I assume it's a typo, since you passed `TRUE` as second argument). After this it's straightforward – Rizier123 Aug 05 '15 at 21:23
  • Yes...that was a typo. The code I actually used is below that and was `decode` – jonmrich Aug 05 '15 at 21:26
  • 1
    `$new_data = $deco_respo['identities']['user_id'];` is very close. Read the dupe and I'm sure you will get this to work by your own! :) – Rizier123 Aug 05 '15 at 21:26
  • I've tried about 15 iterations of this over the past hour including ones like this `$deco_respo->'identities'->'user_id'`. I'd be really grateful if I could just get an answer on this one. – jonmrich Aug 05 '15 at 21:31
  • 1
    1) Do: `$dec_respo = json_decode($response, true); print_r($dec_respo);` 2) Read this answer: http://stackoverflow.com/a/30681087/3933332 Under the point: `Analyse var_dump() / print_r() / var_export() output` and then show what you come up with. – Rizier123 Aug 05 '15 at 21:34
  • I got an even more confusing output from `print_r` and can't quite figure out how to apply what's in your other answer. I thought I had it with `$dec_respo[0]['identities']['user_id']` but got back NULL – jonmrich Aug 05 '15 at 21:42
  • 1
    It's very good and even more close. 1) You realized, that you have 3 dimensions! 2) All keys which you use are correct! 3) BUT, the first two are in the wrong order. Look at your output from `print_r()` again and you will see it. – Rizier123 Aug 05 '15 at 21:44
  • 1
    Well, that almost killed me, but I got it. ; ) Thanks for your help and sticking with me. `($dec_respo['identities'][0]['user_id'])` – jonmrich Aug 05 '15 at 21:47

0 Answers0