2013-04-20 29 views
11

我正在使用Twitter搜索API它會修改一組對象。如果它包含['media']數組,我必須檢查每個對象。以下是具有媒體屬性的對象之一。PHP std :: Class檢查屬性是否存在

object(stdClass)#56 (4) { 
    ["hashtags"]=> 
    array(2) { 
    [0]=> 
    object(stdClass)#57 (2) { 
     ["text"]=> 
     string(9) "katyperry" 
     ["indices"]=> 
     array(2) { 
     [0]=> 
     int(62) 
     [1]=> 
     int(72) 
     } 
    } 
    [1]=> 
    object(stdClass)#58 (2) { 
     ["text"]=> 
     string(8) "katycats" 
     ["indices"]=> 
     array(2) { 
     [0]=> 
     int(73) 
     [1]=> 
     int(82) 
     } 
    } 
    } 
    ["urls"]=> 
    array(0) { 
    } 
    ["user_mentions"]=> 
    array(1) { 
    [0]=> 
    object(stdClass)#59 (5) { 
     ["screen_name"]=> 
     string(13) "perfkatyperry" 
     ["name"]=> 
     string(7) "KatyCat" 
     ["id"]=> 
     int(611836599) 
     ["id_str"]=> 
     string(9) "611836599" 
     ["indices"]=> 
     array(2) { 
     [0]=> 
     int(3) 
     [1]=> 
     int(17) 
     } 
    } 
    } 
    ["media"]=> 
    array(1) { 
    [0]=> 
    object(stdClass)#60 (10) { 
     ["id"]=> 
     int(325747221490434048) 
     ["id_str"]=> 
     string(18) "325747221490434048" 
     ["indices"]=> 
     array(2) { 
     [0]=> 
     int(83) 
     [1]=> 
     int(105) 
     } 
     ["media_url"]=> 
     string(46) "http://pbs.twimg.com/media/BIVJXz-CAAAHQ8M.jpg" 
     ["media_url_https"]=> 
     string(47) "https://pbs.twimg.com/media/BIVJXz-CAAAHQ8M.jpg" 
     ["url"]=> 
     string(22) "http://t.co/RYgQ8Srze5" 
     ["display_url"]=> 
     string(26) "pic.twitter.com/RYgQ8Srze5" 
     ["expanded_url"]=> 
     string(66) "http://twitter.com/perfkatyperry/status/325747221486239744/photo/1" 
     ["type"]=> 
     string(5) "photo" 
     ["sizes"]=> 
     object(stdClass)#61 (5) { 
     ["medium"]=> 
     object(stdClass)#62 (3) { 
      ["w"]=> 
      int(600) 
      ["h"]=> 
      int(600) 
      ["resize"]=> 
      string(3) "fit" 
     } 
     ["thumb"]=> 
     object(stdClass)#63 (3) { 
      ["w"]=> 
      int(150) 
      ["h"]=> 
      int(150) 
      ["resize"]=> 
      string(4) "crop" 
     } 
     ["large"]=> 
     object(stdClass)#64 (3) { 
      ["w"]=> 
      int(1024) 
      ["h"]=> 
      int(1024) 
      ["resize"]=> 
      string(3) "fit" 
     } 
     ["small"]=> 
     object(stdClass)#65 (3) { 
      ["w"]=> 
      int(340) 
      ["h"]=> 
      int(340) 
      ["resize"]=> 
      string(3) "fit" 
     } 
     ["orig"]=> 
     object(stdClass)#66 (3) { 
      ["w"]=> 
      int(1024) 
      ["h"]=> 
      int(1024) 
      ["resize"]=> 
      string(3) "fit" 
     } 
     } 
    } 
    } 
} 

我該怎麼做?

謝謝!

回答

23

使用循環+ property_exists功能:

foreach($myArray as $obj) 
{ 
    if(!property_exists($obj, 'media') || !is_array($obj->media)) 
    { 
    echo("property 'media' does not exist in $obj"); 
    // throw exception, fill in default value for media array, set variable, whatever. 
    } 
} 
+0

很好的回答。它對我有幫助。問候。 – 2015-04-16 09:06:14

+0

怎麼樣!empty($ obj-> media)? – Lamy 2015-06-19 13:13:27

+0

@Lamy:'!empty($ obj-> media)'如果'$ obj-> media'是一個空數組,則評估爲'false';這不是OP想要的。 – 2015-06-20 12:36:57