如何循环多维Instagram数组并获取所有图像的网址?

时间:2016-03-08 09:06:31

标签: php arrays

我想循环遍历下面的数组并回显每个low_resolution url

我已尝试在此上运行foreach循环,但由于它是多维的,我不知道如何访问url值。 (不习惯使用多维数组)

PHP:

public function getInstagramImages() {
        $url = "https://api.instagram.com/v1/users/31008104/media/recent?access_token=31008104.02ed65e.4130309107d34e2cb220d0ef62c2e86d";
        $ch = curl_init($url);

        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);

        $json = curl_exec($ch);
        curl_close($ch);

        $json_array = json_decode($json, true);

        print_r($json_array);
    }

数组:

ArrayArray ( [pagination] => Array ( ) [meta] => Array ( [code] => 200 ) [data] => Array ( [0] => Array ( [attribution] => [tags] => Array ( ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => Amaro [created_time] => 1399119582 [link] => https://www.instagram.com/p/niND4jsISQi7hJjODg-Z6Mm1MaMrY9nOIfiaQ0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10268744_1432980593617900_371121229_n.jpg?ig_cache_key=NzEyMTg5MTMyNTY5MzQ3MjE2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1399119582 [text] => Insane mall! [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 712189132938446543 ) [user_has_liked] => [id] => 712189132569347216_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) ) [1] => Array ( [attribution] => [tags] => Array ( [0] => tailored ) [type] => image [location] => [comments] => Array ( [count] => 0 ) [filter] => 1977 [created_time] => 1398926862 [link] => https://www.instagram.com/p/ncdeeosIU0UR6oYZoy8YMuwAGsM4PPDtW1xUo0/ [likes] => Array ( [count] => 2 ) [images] => Array ( [low_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s320x320/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 320 [height] => 320 ) [thumbnail] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/s150x150/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 150 [height] => 150 ) [standard_resolution] => Array ( [url] => https://scontent.cdninstagram.com/t51.2885-15/e15/10296802_800197019990459_2044337121_n.jpg?ig_cache_key=NzEwNTcyNDc5MDQ1NzMxNjM2.2 [width] => 640 [height] => 640 ) ) [users_in_photo] => Array ( ) [caption] => Array ( [created_time] => 1398926862 [text] => Getting suited up! #tailored [from] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) [id] => 710572479314167115 ) [user_has_liked] => [id] => 710572479045731636_31008104 [user] => Array ( [username] => dj_kp [profile_picture] => https://scontent.cdninstagram.com/t51.2885-19/11906329_960233084022564_1448528159_a.jpg [id] => 31008104 [full_name] => Kieran P ) ) 

数组屏幕截图: enter image description here

仅供参考,阵列比这更进一步。只是展示这部分以尽可能简单。

3 个答案:

答案 0 :(得分:2)

你可以这样做,

foreach($json_array['data'] as $images){
  echo $images['images']['low_resolution']['url'];
}

这将为您提供每个数据数组中的所有low_resolution网址。

答案 1 :(得分:1)

我尝试使用以下代码来获取所有图片标题&网址。

if(is_array($user_recent_data->data)) {
    foreach($user_recent_data->data as $feed_data) {
        if(!empty($feed_data->caption))
        {
            $mediaTitle = $feed_data->caption->text;
        }
        else
        {
            $mediaTitle = '';
        }
        ?>
        <p><?php echo "<img src='".$feed_data->images->thumbnail->url."' style='width:135px; border:1px solid #ccc; padding:3px;'/>";?></p>
        <?php
    }
}
?>

$ user_recent_data是收集instagram数组的可靠地点。

答案 2 :(得分:0)

你需要的是一个递归函数,它会像这样查找url的索引:

function find_urls($array){
  $urls = array();
  foreach ($array as $item){
    if (is_array($item)){
      if(isset($item['url']){
         $urls[] = $item['url'];
      }
      array_merge($urls,find_urls($item));
    }
  }    
  return $urls;
}  

这应该在任何具有任何结构的数组中找到任何名为url的索引;)