是一个完整的" noob"对于php,我试图从具有嵌套数组的json文件中读取数据。这就是我迄今为止所取得的成就。 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"
}
}
]
}
]
}
和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>
使用此代码,我可以正确显示<H2>
和<span>
中的数据,但是&#39; url&#39;每个人都是一样的......那是什么才能得到那个?
非常感谢 - 它可能非常明显,但是非常感谢任何帮助!
答案 0 :(得分:1)
这应该会给你一个想法。不是很好,但它应该工作。问题是你只迭代第一级(活动)。你也应该循环第二级(parcs)。所以你应该至少有两个foreach循环:
<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>
因此,活动是您的第一个数组,每个活动都包含一个包含parcs的数组,您还应该遍历每个元素。
在您当前的解决方案中,您确实选择了每个循环中的第一个parc,同时迭代每个活动:$obj['parcs']['0']['nom']
,但0
在每个循环中保持0
,这就是原因为什么你只看到第一个parc。
Parcs也是一个数组,所以你也应该迭代这些元素,就像你在活动中一样。