1

I have a json file and I want to display its data in a PHP file. I use the below code but it gives me error.

$json = file_get_contents('data.json'); 
$data = json_decode($json);
foreach ($data as $value)
  {
  echo "$value[1]<br>";
  }

data.json file contains data in this format.

{
"users":[
{"id":"03B7F72C1A522631","user":"[email protected]"}, 
{"id":"27EB9CE8338083AE","user":"[email protected]"}, 
{"id":"E27854ABBFF8CD92","user":"[email protected]"}],
"status":
    {
    "version":"0.9.5.0",
    "command":"listusers",
    "opf":"json",
    "error":false,
    "code":0
    }
}

I want the Output as

User ID | User Email

03B7F72C1A522631 | [email protected]
27EB9CE8338083AE | [email protected]
2
  • json_decode($json, true); add second parameter if you want to use as array. Commented Nov 24, 2013 at 12:43
  • What do you mean by "it gives me error"? Is there any error message which you've forgot to share? Commented Apr 25, 2019 at 10:29

3 Answers 3

4
$json = file_get_contents('data.json'); 
$data = json_decode($json,true);
$users=$data['users'];
for($users as $user)
{
   echo $user['id']." ".$user['user'];
}
$status=$data['status'];
Sign up to request clarification or add additional context in comments.

Comments

0

Use this which will give an associative array instead of an object

$data = json_decode($json,true);

for details have a look here json_decode function

3 Comments

Now it gives this output "Array"
print_r($data) and see the full array
Thanks for your help, I update the question and I mentioned the desired output
0

get data from json file

$homepage = file_get_contents('url');

$someArray = json_decode($homepage, true);

foreach ($someArray as $key => $value) {
    echo "\nAddress: " . $value["address"] . "\n\n";
}

1 Comment

Can you explain that further? What makes you think that your code solves the mysterious error?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.