将JSON转换为PHP数组和Echo?

时间:2017-10-31 16:34:21

标签: php json

这是我的PHP代码:

<?php
$json_url = "REDACTED";
$json = file_get_contents($json_url);

$stats = json_decode($json, true);

    foreach ($stats as $row) {
        echo $row['results']['result']['conversions'] . "<br />";
    }
?>

这是JSON:

{
   "metadata":{
      "iserror":"false",
      "responsetime":"0.07s"
   },
   "results":{
      "first":1,
      "last":99,
      "total":99,
      "result":[
         {
            "total_visitors":"3",
            "visitors":"3",
            "conversions":"0"
         },
         {
            "total_visitors":"26",
            "visitors":"26",
            "conversions":"0"
         },
         {
            "total_visitors":"13",
            "visitors":"13",
            "conversions":"0"
         },
         {
            "total_visitors":"1",
            "visitors":"1",
            "conversions":"0"
         },
         {
            "total_visitors":"1",
            "visitors":"1",
            "conversions":"0"
         }
      ]
   }
}

基本上我只是试图回应&#34;转换&#34;来自json文件中的每个部分。

从未使用过PHP的JSON文件,所以我不确定我在哪里出错。

3 个答案:

答案 0 :(得分:1)

在访问数组时需要小幅修正

foreach ($stats['results']['result'] as $row) {
        echo $row['conversions'] . "<br />";
}

因为当你执行json_decode时,你会得到如下所示的数组

Array
(
    [metadata] => Array
        (
            [iserror] => false
            [responsetime] => 0.07s
        )

    [results] => Array
        (
            [first] => 1
            [last] => 99
            [total] => 99
            [result] => Array
                (
                    [0] => Array
                        (
                            [total_visitors] => 3
                            [visitors] => 3
                            [conversions] => 0
                        )

                    [1] => Array
                        (
                            [total_visitors] => 26
                            [visitors] => 26
                            [conversions] => 0
                        )

                    [2] => Array
                        (
                            [total_visitors] => 13
                            [visitors] => 13
                            [conversions] => 0
                        )

                    [3] => Array
                        (
                            [total_visitors] => 1
                            [visitors] => 1
                            [conversions] => 0
                        )

                    [4] => Array
                        (
                            [total_visitors] => 1
                            [visitors] => 1
                            [conversions] => 0
                        )

                )

        )

)

答案 1 :(得分:0)

应该如下:

<?php
$json_url = "REDACTED";
$json = file_get_contents($json_url);

$stats = json_decode($json, true);
if ($stats && isset($stats['results']) && isset($stats['results']['result'])) {
    foreach ($stats['results']['result'] as $row) {
        echo $row['conversions'] . "<br />";
    }
}
?>

因此,检查是否需要在JSON中设置字段,然后循环每个result记录以获取conversions

答案 2 :(得分:0)

$ stats = json_decode($ json,true);

foreach ($stats['results']['result'] as $row) {
            echo $row['conversions'] . "<br />";
    }