我正在尝试使用Json_decode PHP解码json ...但是收到错误为NULL JSON文件:
{"elements":[
{"courseType":"v2.ondemand",
"id":"69Bku0KoEeWZtA4u62x6lQ",
"slug":"gamification",
"name":"Gamification"
},
{"courseType":"v2.ondemand",
"id":"0HiU7Oe4EeWTAQ4yevf_oQ",
"slug":"missing-data",
"name":"Dealing With Missing Data"
},
{"courseType":"v2.ondemand",
"id":"5zjIsJq-EeW_wArffOXkOw",
"slug":"vital-signs",
"name":"Vital Signs: Understanding What the Body Is Telling Us"
},
{"courseType":"v2.ondemand",
"id":"v9CQdBkhEeWjrA6seF25aw",
"slug":"modern-art-ideas",
"name":"Modern Art & Ideas"
},{"courseType":"v2.ondemand",
"id":"QgmoVdT2EeSlhSIACx2EBw",
"slug":"evolvinguniverse",
"name":"The Evolving Universe"
}
我只需要选择ID,名称和课程类型
<?php
$strings = file_get_contents("https://api.coursera.org/api/courses.v1");
$arrays = json_decode($strings,true);
?>
<div class="table-responsive">
<table class="table table-bordered table-striped" id="coursedata">
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Type</th>
</tr>
<tr>
<td><?php print_r($arrays['elements']['id']);?></td>
</tr>
</table>
</div>
答案 0 :(得分:2)
您可以使用foreach loop
[1]遍历结果并将它们打印在表格中:
<?php
$strings = file_get_contents("https://api.coursera.org/api/courses.v1");
$arrays = json_decode($strings,true);
?>
<div class="table-responsive">
<table class="table table-bordered table-striped" id="coursedata">
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Type</th>
</tr>
<?php
foreach($arrays['elements'] as $element) {
echo '<tr>';
echo '<td>' . $element['id'] . '</td>';
echo '<td>' . $element['name'] . '</td>';
echo '<td>' . $element['courseType'] . '</td>';
echo '</tr>';
}
?>
</table>
</div>
PS:您可能想看看heredoc [2],用于输出带有PHP变量的HTML。
答案 1 :(得分:0)
您需要使用数组索引号访问数组数据:
<?php
$strings = file_get_contents("https://api.coursera.org/api/courses.v1");
$arrays = json_decode($strings,true);
?>
<div class="table-responsive">
<table class="table table-bordered table-striped" id="coursedata">
<tr>
<th>Course ID</th>
<th>Course Name</th>
<th>Course Type</th>
</tr>
<?php for($i=0; $i<count($arrays); $i++) { ?>
<tr>
<td><?php echo $arrays['elements'][$i]['id'];?></td>
<td><?php echo $arrays['elements'][$i]['name'];?></td>
<td><?php echo $arrays['elements'][$i]['courseType'];?></td>
</tr>
<?php } ?>
</table>
</div>
答案 2 :(得分:0)
尝试使用此代码json_decode
<?php
$jsonStr = '{"elements":[{"courseType":"v2.ondemand","id":"69Bku0KoEeWZtA4u62x6lQ","slug":"gamification","name":"Gamification"},{"courseType":"v2.ondemand","id":"0HiU7Oe4EeWTAQ4yevf_oQ","slug":"missing-data","name":"Dealing With Missing Data"},{"courseType":"v2.ondemand","id":"5zjIsJq-EeW_wArffOXkOw","slug":"vital-signs","name":"Vital Signs: Understanding What the Body Is Telling Us"},{"courseType":"v2.ondemand","id":"v9CQdBkhEeWjrA6seF25aw","slug":"modern-art-ideas","name":"Modern Art & Ideas"},{"courseType":"v2.ondemand","id":"QgmoVdT2EeSlhSIACx2EBw","slug":"evolvinguniverse","name":"The Evolving Universe"}]}';
$array = json_decode($jsonStr,true);
?>
<div class="table-responsive">
<table class="table table-bordered table-striped" id="coursedata" border='1'>
<tr>
<th>
Course ID
</th>
<th>
Course Name
</th>
<th>
Course Type
</th>
</tr>
<?php
foreach($array['elements'] as $k=>$v){
?>
<tr>
<td><?php echo $v['id'];?></td>
<td><?php echo $v['name'];?></td>
<td><?php echo $v['courseType'];?></td>
</tr>
<?php
}
?>
</table>
</div>