benp benp - 6 months ago 13
JSON Question

PHP looping through nested json array

being a complete "noob" to php, I'm trying to read data from a json file which has nested arrays. Here's what I've been able to achieve so far.
json:

{
"activites": [
{
"nom": "Observation des oiseaux",
"image": "http://cdn2.gossipcenter.com/sites/default/files/imagecache/story_header/photos/tom-cruise-020514sp.jpg",
"description": "Sed id eros nec orci elementum auctor at vitae tortor. Pellentesque eget nibh sed justo consequat suscipit. Nunc augue sem, porta non molestie sed, volutpat quis orci. Curabitur in erat eu nibh fermentum volutpat vitae ut nisi. Nullam vitae euismod mauris. Suspendisse pretium facilisis gravida. Duis magna arcu, pharetra in sapien eu, ornare vehicula arcu.",
"parcs": [
{
"nom": "Nom du parc 1",
"url": "url-du-parc-1.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 2",
"url": "url-du-parc-2.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 3",
"url": "url-du-parc-3.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 4",
"url": "url-du-parc-3.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
}
]
},
{
"nom": "Pétanque",
"image": "http://cdn2.gossipcenter.com/sites/default/files/imagecache/story_header/photos/tom-cruise-020514sp.jpg",
"description": "Sed id eros nec orci elementum auctor at vitae tortor. Pellentesque eget nibh sed justo consequat suscipit. Nunc augue sem, porta non molestie sed, volutpat quis orci. Curabitur in erat eu nibh fermentum volutpat vitae ut nisi. Nullam vitae euismod mauris. Suspendisse pretium facilisis gravida. Duis magna arcu, pharetra in sapien eu, ornare vehicula arcu.",
"parcs": [
{
"nom": "Nom du parc 1 pour deuxième activité",
"url": "url-du-parc-1.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 2 pour deuxième activité",
"url": "url-du-parc-2.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
}
]
},
{
"nom": "Nom de l'activité 3",
"image": "http://cdn2.gossipcenter.com/sites/default/files/imagecache/story_header/photos/tom-cruise-020514sp.jpg",
"description": "Sed id eros nec orci elementum auctor at vitae tortor. Pellentesque eget nibh sed justo consequat suscipit. Nunc augue sem, porta non molestie sed, volutpat quis orci. Curabitur in erat eu nibh fermentum volutpat vitae ut nisi. Nullam vitae euismod mauris. Suspendisse pretium facilisis gravida. Duis magna arcu, pharetra in sapien eu, ornare vehicula arcu.",
"parcs": [
{
"nom": "Nom du parc 1 pour troisième activité",
"url": "url-du-parc-1.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 2 pour troisième activité",
"url": "url-du-parc-2.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
},
{
"nom": "Nom du parc 3 pour troisième activité",
"url": "url-du-parc-2.html",
"location":
{
"latitude": "44.7789990",
"longitude": "67.9988990"
}
}
]
}
]
}


and the php:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="">
<title></title>
<link href="assets/css/styles.css" rel="stylesheet">
<!--[if IE]>
<link rel="stylesheet" type="text/css" href="assets/css/ie.css" />
<![endif]-->
</head>
<body>
<?php
$json_url = "./assets/js/activites.json";
$json = file_get_contents($json_url);
$stuff = json_decode($json, true);
?>
<div class="toto">
<?php
foreach($stuff['activites'] as $obj){
?>
<h2>
<?php echo $obj['nom'] ?></h2>
<a href="<?php echo $obj['parcs']['0']['url'] ?>
">
<span>
<?php echo $obj['parcs']['0']['nom'] ?></span>
</a>
<?php
}
?></div>
</body>
</html>


With this code I can display the data in
<H2>
and
<span>
correctly, but the 'url' is the same for each... What would be the right way to get that?
Many thanks in advance - it's probably very obvious but any help is much appreciated!

Answer

Well this should give you an idea. Not very nice, but it should work. The problem is you only iterate over the first level (activities). You should also loop over the second level (parcs). So you should have at least two foreach loops:

 <div class="toto">
    <?php
    foreach($stuff['activites'] as $obj){ // over here you iterate over the first level ?>

      <h2><?php echo $obj['nom']; ?></h2>

      <?php foreach($obj['parcs'] as $parc){ // this is the second level array, the parcs

         <a href="<?php echo $parc['url']; ?>">
             <span><?php echo $parc['nom']; ?></span>
         </a>

    <?php
      }
    }
?></div>

So the activities is your first array and each activity contains an array with parcs, where you also should loop over each element.

In your current solution, you do select the first parc in each loop, while iterating over each activity: $obj['parcs']['0']['nom'], but the 0 stays 0 in each loop, that's the reason why you only see the first parc.

Parcs is an array as well, so you should iterate over those elements as well, like you did with activities.