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.
-
Why do you think something is wrong? Do you get an error?Felix Kling– Felix Kling2017年10月31日 16:38:03 +00:00Commented Oct 31, 2017 at 16:38
-
Because nothing is being echoed.Chase– Chase2017年10月31日 16:38:56 +00:00Commented Oct 31, 2017 at 16:38
-
Possible duplicate of How do I extract data from JSON with PHP?Felix Kling– Felix Kling2017年10月31日 16:40:01 +00:00Commented Oct 31, 2017 at 16:40
3 Answers 3
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
Akshay Hegde
17.1k2 gold badges25 silver badges39 bronze badges
Sign up to request clarification or add additional context in comments.
3 Comments
Chase
Perfect, will accept answer in 8 minutes when it allows me to :D
Chase
Thanks, pretty sure that would've taken me hours to figure out.
Akshay Hegde
Not an issue happens in beginning days of programming, happened with all of us :)
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
Codemole
3,2095 gold badges28 silver badges42 bronze badges
Comments
$stats = json_decode($json, true);
foreach ($stats['results']['result'] as $row) {
echo $row['conversions'] . "<br />";
}
answered Oct 31, 2017 at 16:52
jvk
2,1893 gold badges22 silver badges28 bronze badges
Comments
lang-php