Jeroen
Jeroen

Reputation: 472

Read out a json file php

Hi I have this json code

{"points":[ {"10":"10"}, {"1":"1"} ]}

and this is my php code

$pointsfirst = $row['points'];
$points = json_decode($pointsfirst,true);
$getit = $points['points'][1]['10'];
echo $getit;

$row['points'] is from my database where I have stored the json

and I keep getting this error

Notice: Undefined offset: 10 in /Applications/MAMP/htdocs/projectg/getpointsapi.php on line 46

what am I doing wrong ??

Upvotes: 0

Views: 61

Answers (3)

Dmitrii Cheremisin
Dmitrii Cheremisin

Reputation: 1568

Right way is:

$getit = $points['points'][0][10];
echo $getit;

Upvotes: 2

Sjon
Sjon

Reputation: 5155

Arrays in PHP start with offset 0, not 1. This means you should probably use $getit = $points['points'][0]['10']; instead.

Upvotes: 0

Alexander Kampf
Alexander Kampf

Reputation: 34

Try $getit = $points['points']['1']['10'];

with the quotas! instead...

Upvotes: 0

Related Questions