I have a jSON file and I want to display its data but it show me result in some strange format i don't know where I'm wrong. Here is my PHP code,
$json = file_get_contents('data.json');
$data = json_decode($json,true);
$users = $data['user'];
foreach($users as $user)
{
echo $user['user'];
}
Data in my jSON file below,
{
"user":
{
"id":"#79F9FFB1EE0CB1CC",
"user":"test@mail.com",
"password":"123456",
"email":"test@mail.com",
"name":"John Doe",
"creationDate":1387111401
},
"status":
{
"version":"0.9.9.1",
"command":"getuser",
"opf":"json",
"error":false,
"code":0
}
}
It's your foreach loop that is wrong.
You only have on user so replace your loop by :
$users = $data['user'];
echo $users['user'];
$json = '{
"user":
{
"id":"#79F9FFB1EE0CB1CC",
"user":"test@mail.com",
"password":"123456",
"email":"test@mail.com",
"name":"John Doe",
"creationDate":1387111401
},
"status":
{
"version":"0.9.9.1",
"command":"getuser",
"opf":"json",
"error":false,
"code":0
}
}';
$data = json_decode($json,true);
$users = $data['user'];
foreach($users as $key=>$user)
{
echo $user.'<br>';
}