如何在php中过滤数组(stdClass)

时间:2016-12-14 09:40:22

标签: php

我需要过滤一个数组才能得到“代码”

array (size=17)
  0 => 
    object(stdClass)[5]
      public 'code' => string 'BN_L-_agmgt' (length=11)
      public 'dimensions' => 
        object(stdClass)[6]
          public 'width' => int 1080
          public 'height' => int 810
      public 'comments_disabled' => boolean false
      public 'owner' => 
        object(stdClass)[7]
          public 'id' => string '301290076' (length=9)
      public 'comments' => 
        object(stdClass)[8]
          public 'count' => int 0
      public 'caption' => string '#lights #beautiful #piazza #plebiscito #photo #followme #likeforlike' (length=68)
      public 'likes' => 
        object(stdClass)[9]
          public 'count' => int 21
      public 'date' => int 1481696475
      public 'thumbnail_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c135.0.810.810/14624676_588980267967030_6184721183611551744_n.jpg?ig_cache_key=MTQwNDg5NDMxNTk3MjU1Mjc0OQ%3D%3D.2.c' (length=187)
      public 'is_video' => boolean false
      public 'id' => string '1404894315972552749' (length=19)
      public 'display_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/e35/14624676_588980267967030_6184721183611551744_n.jpg?ig_cache_key=MTQwNDg5NDMxNTk3MjU1Mjc0OQ%3D%3D.2' (length=154)
  1 => 
    object(stdClass)[10]
      public 'code' => string 'BN-eRGQh8IK' (length=11)
      public 'dimensions' => 
        object(stdClass)[11]
          public 'width' => int 750
          public 'height' => int 538
      public 'comments_disabled' => boolean false
      public 'owner' => 
        object(stdClass)[12]
          public 'id' => string '311016089' (length=9)
      public 'comments' => 
        object(stdClass)[13]
          public 'count' => int 2
      public 'caption' => string '#plebiscito' (length=11)
      public 'likes' => 
        object(stdClass)[14]
          public 'count' => int 24
      public 'date' => int 1481672506
      public 'thumbnail_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c147.0.750.750/15338447_1774364399481982_8165079596765544448_n.jpg?ig_cache_key=MTQwNDY5MzI1MDEzMjM5NDUwNg%3D%3D.2.c' (length=188)
      public 'is_video' => boolean false
      public 'id' => string '1404693250132394506' (length=19)
      public 'display_src' => string '_n.jpg?ig_cache_key=MTQwNDY5MzI1MDEzMjM5NDUwNg%3D%3D.2' (length=171)
  2 => 
    object(stdClass)[15]
      public 'code' => string 'BN-XEcDjd77' (length=11)
      public 'dimensions' => 
        object(stdClass)[16]
          public 'width' => int 1080
          public 'height' => int 1243
      public 'comments_disabled' => boolean false
      public 'owner' => 
        object(stdClass)[17]
          public 'id' => string '301158275' (length=9)
      public 'comments' => 
        object(stdClass)[18]
          public 'count' => int 0
      public 'caption' => string '#piazza #plebiscito #napoli #palazzoreale #luxory #aristocratlife #pieceofhistory #travel #admire #beauty #art #architecture #archilovers #culturalheritage #loveit #details #amazing #place  #foreshortening #interior #design #marble #plaster #decoration #whitepower #entrance' (length=275)
      public 'likes' => 
        object(stdClass)[19]
          public 'count' => int 26
      public 'date' => int 1481668732
      public 'thumbnail_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/c0.81.1080.1080/15306100_218138875299871_5131961228335251456_n.jpg?ig_cache_key=MTQwNDY2MTU5Mzg1ODIzNjE1NQ%3D%3D.2.c' (length=188)
      public 'is_video' => boolean false
      public 'id' => string '1404661593858236155' (length=19)
      public 'display_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/e35/15306100_218138875299871_5131961228335251456_n.jpg?ig_cache_key=MTQwNDY2MTU5Mzg1ODIzNjE1NQ%3D%3D.2' (length=154)
  3 => 
    object(stdClass)[20]
      public 'code' => string 'BN-KM1wDM3I' (length=11)
      public 'dimensions' => 
        object(stdClass)[21]
          public 'width' => int 1080
          public 'height' => int 1080
      public 'comments_disabled' => boolean false
      public 'owner' => 
        object(stdClass)[22]
          public 'id' => string '584137256' (length=9)
      public 'comments' => 
        object(stdClass)[23]
          public 'count' => int 1
      public 'caption' => string '#pizza#pizzachebonta#napoli#naples#foto_napoli#ioscatto#vivonapoli#italia#italy#plebiscito#gambrinus#rossopomodoro#campania#instamoment#instagood#instagnam#photooftheday#love#simmenapulpaisà ' (length=191)
      public 'likes' => 
        object(stdClass)[24]
          public 'count' => int 27
      public 'date' => int 1481661985
      public 'thumbnail_src' => string 'https://instagram.fclo1-1.fna.fbcdn.net/t51.2885-15/s640x640/sh0.08/e35/15337164_241291976302551_5452823035130150912_n.jpg?ig_cache_key=MTQwNDYwNDk5NjU5OTQ2NzQ2NA%3D%3D.2' (length=170)
      public 'is_video' => boolean false
      public 'id' => string '1404604996599467464' (length=19)
      public 'display_src' => string 'https://2_n.jpg?ig_cache_key=MTQwNDYwNDk5NjU5OTQ2NzQ2NA%3D%3D.2' (length=154)

感谢您的帮助。 我的打印数组代码: 代码是从instagram过滤数据,但正如您将看到的,我尝试使用var_dump并以此方式过滤我。

<?php

$baseUrl = 'https://www.instagram.com/explore/tags/plebiscito/?__a=1';
$url = $baseUrl;
$count = 0;
$array = array();    

while(1) {
    $json = json_decode(file_get_contents($url));

    var_dump(array_filter($json->tag->media->nodes)); // working
    if(!$json->tag->media->page_info->has_next_page) break;
    $url = $baseUrl.'&max_id='.$json->tag->media->page_info->end_cursor;
    ++$count; 
}
?>

我想不出任何简单打印代码的想法。

1 个答案:

答案 0 :(得分:0)

假设在你的代码中,array_filter($json->tag->media->nodes)返回上面的数组,你可以这样做来获取这个数组并得到'code'值:

<?php

$baseUrl = 'https://www.instagram.com/explore/tags/plebiscito/?__a=1';
$url = $baseUrl;
$count = 0;
$array = array();    

$json = json_decode(file_get_contents($url));

$filter_array = array_filter($json->tag->media->nodes)); // working
foreach($filter_array as $object_return){
    echo $object_return->code.'</br>'; // will output code string 
}

if(!$json->tag->media->page_info->has_next_page) break;
$url = $baseUrl.'&max_id='.$json->tag->media->page_info->end_cursor;
++$count; 

我还删除了此处不需要的while循环。