Skip to content
Advertisement

Keep getting Undefiend Index when using json_decode

I’m working with an API and I keep getting Undefined Index when trying to echo the data out,

If I use this code:-

JavaScript

I get this output:-

JavaScript

And then if I add this to the end of the code

JavaScript

I get a “Notice: Undefined index: registration”

Looking at the docs for json_decode I cannot see what I am doing wrong. On another API I have used I noticed it was using CURLOPT_CUSTOMREQUEST => “POST”, and had this line underneath CURLOPT_POSTFIELDS => json_encode([‘registration’ => $registration]),

And it would echo out. This one wont and the only difference I see is the GET

So has it got something to do with the CURLOPT_CUSTOMREQUEST => “GET”, line to why I cannot echo anything out?

Thanks in advance for any help

Advertisement

Answer

If the answer is “[{something: whatever}]”, you’re getting an array with an object inside.

So, your registration is actually $info[0]['registration'].

Or you can extract it by first doing $info = $info[0].

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement