-1

I have a PHP variable that when I echo it will display an array that looks like this:

My Variable:

echo $mayVariable;

displays:

{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}

I need to get the value from id within that array.

So I tried this:

echo $mayVariable[0]['id'];

But this doesn't give me anything.

I also tried:

echo $mayVariable['data']['id'];

and still I don't get anything in the echo...

Could someone please advise on this issue?

Jackson
  • 800
  • 16
  • 36

1 Answers1

1

This JSON is an object array after decode it generally.

$json = '{"data":[{"id":"4756756575","name":"David","url":"https:\/\/www.somesite.com"}],"page":false}';

$arr = json_decode($json);

echo $arr->data[0]->id;//4756756575

If you use true as second parameter then:

$arr = json_decode($json, true);

echo $arr['data'][0]['id'];//4756756575
Murad Hasan
  • 9,565
  • 2
  • 21
  • 42