使用PHP从JSON获取年份属性

使用PHP从JSON获取年份属性,php,json,Php,Json,我已经成功地从这个JSON页面检索到了两个属性,但是我在获取“years”属性时遇到了麻烦。我想问题是我缺少一些父元素。如果有人能给我指出正确的方向,我将不胜感激,谢谢。这是具有JSON结构的链接: 您可以在中看到API的结果 代码如下: <?php $datvin=""; $title=""; $posts=""; $mds=""; // copy file content into a string var if (isset($_POST['datvin'])){ $datvin=t

我已经成功地从这个JSON页面检索到了两个属性,但是我在获取“years”属性时遇到了麻烦。我想问题是我缺少一些父元素。如果有人能给我指出正确的方向,我将不胜感激,谢谢。这是具有JSON结构的链接:

您可以在中看到API的结果

代码如下:

<?php
$datvin="";
$title="";
$posts="";
$mds="";
// copy file content into a string var
if (isset($_POST['datvin'])){
$datvin=trim($_POST['datvin']);
$json_file = file_get_contents('http://api.edmunds.com/api/vehicle/v2/vins/'.$datvin.'?fmt=json&api_key=XXX');
// convert the string to a json object
$jfo = json_decode($json_file);
// read the title value
$title = $jfo->make->name;
// copy the posts array to a php var
$posts = $jfo->make->id;
$mds= $jfo->years->year;

}
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" href="css/style.css" />
</head>
<body>
<div class="container">
<div class="header">

</div><!-- header -->
<h1 class="main_title"><?php echo $title; ?></h1>
<div class="content">
<form method="post">
<input type="text" name="datvin"><br>
<input type="submit">
</form>
<ul class="ul_json clearfix">
<?php

echo "$title<br>";
echo "$posts<br>";
echo "$mds";
?>
</ul>
</div><!-- content -->
<div class="footer">

</div><!-- footer -->
</div><!-- container -->
</body>
</html>



年份是一个数组。尝试
$mds=$jfo->years[0]->year

好吧,最好看到使用var_dump($jfo)的最终数组;为了看看它是什么样子……用户把API键留在了他的OP中,我已经看到了它的结构。是的,我之前刚检查过,但我从来没有把它拆开,我就是这么做的。:)真是个好发现!我刚刚运行了@Styphon的建议,并确认这是答案。