[英]echo an array number with a variable
I have created an array from a json file and can call thumbnails/ titles just fine. 我从json文件创建了一个数组,可以很好地调用缩略图/标题。
<?php
$file_contents = file_get_contents("http://vimeo.com/api/v2/username/videos.json");
$data = json_decode($file_contents);
$image= $data[0]->{'thumbnail_large'};
$title= $data[1]->{'title'};
?>
<?php
echo '<img src="'.$image.'"/>';
echo $title;
?>
However I want to echo the array number with $image so that i dont have to create multiple variables ie $image1 $image2 etc for each thumbnail or title. 但是我想用$ image回显数组编号,这样我就不必为每个缩略图或标题创建多个变量,即$ image1 $ image2等。
below is a version of the above code were i used $num to try to illustrate what I mean. 下面是上面代码的一个版本,我用$ num来说明我的意思。
<?php
$file_contents = file_get_contents("http://vimeo.com/api/v2/olouali/videos.json");
$data = json_decode($file_contents);
$image[$num]= $data[$num]->{'thumbnail_large'};
?>
<?php
echo '<img src="'.$image[$num].'"/>';
?>
I'm not sure if this is the right approach to what I'm trying to create as I'm still learning php. 我不确定这是否是我尝试学习php的正确方法。
Try this: 尝试这个:
<?php
$file_contents = file_get_contents("http://vimeo.com/api/v2/olouali/videos.json");
$data = json_decode($file_contents);
$count = count($data);
for($i = 0; $i < $count; $i++) {
echo '<img src="'.$data[$i]->{'thumbnail_large'}.'"/>';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.