Parse JSON data in PHP

json_decode

$data='{"id":1,"firstname":"george","lastname":"karpouzas","emails":["abcd@gmail.com","abcd@yahoo.com"]}';
$obj=json_decode($data);
echo 'ID: <b>'.$obj->id.'</b><br>';
echo 'First name: <b>'.$obj->firstname.'</b><br>';
echo 'Last name: <b>'.$obj->lastname.'</b><br>';
echo 'Email 1: <b>'.$obj->emails[0].'</b><br>';
echo 'Email 2: <b>'.$obj->emails[1].'</b><br>';

Output:

ID: 1
First name: george
Last name: karpouzas
Email 1: abcd@gmail.com
Email 2: abcd@yahoo.com

Categories: PHP Tags: , , , , ,
%d bloggers like this: