1

Here's my PHP code:

<?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 />";
    }
?>

Here's the 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"
         }
      ]
   }
}

Essentially I'm just trying to echo the "conversions" from each section in the json file.

Never worked with a JSON file using PHP for, so I'm not really sure where I'm going wrong with this.

Chase
  • 115
  • 2
  • 8

3 Answers3

1

Need small correction in accessing array like below

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

Because when you do json_decode, you will get array like below

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
                        )

                )

        )

)
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
0

It should be like following:

<?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 />";
    }
}
?>

So check if needed fields are set in JSON, and then loop for every result record to get the conversions.

Codemole
  • 3,069
  • 5
  • 25
  • 41
0

$stats = json_decode($json, true);

foreach ($stats['results']['result'] as $row) {
            echo $row['conversions'] . "<br />";
    }
jvk
  • 2,133
  • 3
  • 19
  • 28