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.

asked Oct 31, 2017 at 16:34
3
  • Why do you think something is wrong? Do you get an error? Commented Oct 31, 2017 at 16:38
  • Because nothing is being echoed. Commented Oct 31, 2017 at 16:38
  • Possible duplicate of How do I extract data from JSON with PHP? Commented Oct 31, 2017 at 16:40

3 Answers 3

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
 )
 )
 )
)
answered Oct 31, 2017 at 16:38
Sign up to request clarification or add additional context in comments.

3 Comments

Perfect, will accept answer in 8 minutes when it allows me to :D
Thanks, pretty sure that would've taken me hours to figure out.
Not an issue happens in beginning days of programming, happened with all of us :)
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.

answered Oct 31, 2017 at 16:39

Comments

0

$stats = json_decode($json, true);

foreach ($stats['results']['result'] as $row) {
 echo $row['conversions'] . "<br />";
 }
answered Oct 31, 2017 at 16:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.