简体   繁体   中英

echo an array number with a variable

I have created an array from a json file and can call thumbnails/ titles just fine.

<?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.

below is a version of the above code were i used $num to try to illustrate what I mean.

<?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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM