Php 如何在多维Instagram阵列上循环并获取所有图像的url?
我想在下面的数组上循环并回显每个Php 如何在多维Instagram阵列上循环并获取所有图像的url?,php,arrays,Php,Arrays,我想在下面的数组上循环并回显每个低分辨率url 我已经尝试过在上面运行foreach循环,但是由于它是多维的,我不确定如何访问url值。(不习惯使用多维数组) PHP: public function getInstagramImages() { $url = "https://api.instagram.com/v1/users/31008104/media/recent?access_token=31008104.02ed65e.4130309107d34e2cb220d0e
低分辨率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 ) )
数组:
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 ) )
阵列的屏幕截图:
仅供参考,阵列将继续进行。仅显示此部分以使其尽可能简单。您可以这样做
foreach($json_array['data'] as $images){
echo $images['images']['low_resolution']['url'];
}
这将为您提供每个数据数组中的所有低分辨率url。您需要的是一个递归函数,它查找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的索引;) 我尝试使用以下代码获取所有图像标题和URL
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
}
}
?>
if(是数组($user\u recent\u data->data)){
foreach($user\u recent\u data->data as$feed\u data){
如果(!空($feed_data->caption))
{
$mediaTitle=$feed_data->caption->text;
}
其他的
{
$mediaTitle='';
}
?>
这是在使用对象,对吗?但是我用这个方法实现了。谢谢:)