用变量回显数组编号


echo an array number with a variable

>我已经从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; 
    ?>

但是,我想用$image回显数组编号,这样我就不必为每个缩略图或标题创建多个变量,即 $image 1 $image 2 等。

下面是我$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].'"/>';
            ?>

不确定这是否是我尝试创建的正确方法,因为我仍在学习 php。

试试这个:

<?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'}.'"/>';
}