Chase
Chase

Reputation: 135

Convert JSON to PHP Array and Echo?

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.

Upvotes: 1

Views: 43

Answers (3)

jvk
jvk

Reputation: 2201

$stats = json_decode($json, true);

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

Upvotes: 0

Codemole
Codemole

Reputation: 3189

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.

Upvotes: 0

Akshay Hegde
Akshay Hegde

Reputation: 16997

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
                        )

                )

        )

)

Upvotes: 1

Related Questions