使用 PHP 显示 Json 数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20174588/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 02:57:18  来源:igfitidea点击:

Display Json data using PHP

phparraysjson

提问by Arif

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 文件,我想在 PHP 文件中显示它的数据。我使用下面的代码,但它给了我错误。

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

data.jsonfile contains data in this format.

data.json文件包含这种格式的数据。

{
"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]

回答by Patato

$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'];

回答by Suvash sarker

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

详情请看这里json_decode 函数

回答by akshay borase

get data from json file

从json文件中获取数据

$homepage = file_get_contents('url');

$someArray = json_decode($homepage, true);

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