2011-12-29 31 views
11

Tôi không quá quen thuộc với các đối tượng, tôi thường chuyển đổi chúng thành mảng bằng cách sử dụng tập lệnh, nhưng tôi nghĩ đã đến lúc để tạo dựng và tìm hiểu điều này, vì vậy tôi hỏi làm thế nào tôi có thể lặp lại một mảng các đối tượng và kéo ra các thông tin cụ thể tôi cần, ví dụ về những gì đang được trả về là dưới đây.Làm thế nào để lặp qua các đối tượng trong php

[2] => stdClass Object 
     (
      [id_str] => 152240682071900161 
      [in_reply_to_status_id] => 
      [contributors] => 
      [place] => 
      [truncated] => 
      [geo] => 
      [favorited] => 
      [created_at] => Thu Dec 29 04:13:01 +0000 2011 
      [user] => stdClass Object 
       (
        [id_str] => 71876190 
        [contributors_enabled] => 
        [lang] => en 
        [protected] => 
        [url] => http://www.imdb.com/name/nm0000362/ 
        [default_profile] => 1 
        [profile_use_background_image] => 1 
        [name] => Danny DeVito 
        [default_profile_image] => 
        [friends_count] => 14 
        [profile_text_color] => 333333 
        [statuses_count] => 5361 
        [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png 
        [utc_offset] => -28800 
        [description] => I'm an actor, director and producer. 
        [is_translator] => 
        [created_at] => Sat Sep 05 19:44:48 +0000 2009 
        [profile_link_color] => 0084B4 
        [following] => 1 
        [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png 
        [favourites_count] => 8 
        [follow_request_sent] => 
        [geo_enabled] => 1 
        [profile_background_color] => C0DEED 
        [profile_background_tile] => 
        [followers_count] => 1784732 
        [profile_image_url] => http://a2.twimg.com/profile_images/400232403/Photo_6_normal.jpg 
        [screen_name] => DannyDeVito 
        [show_all_inline_media] => 
        [profile_sidebar_fill_color] => DDEEF6 
        [profile_image_url_https] => https://si0.twimg.com/profile_images/400232403/Photo_6_normal.jpg 
        [listed_count] => 20918 
        [notifications] => 
        [location] => Los Angeles 
        [id] => 71876190 
        [verified] => 1 
        [time_zone] => Pacific Time (US & Canada) 
        [profile_sidebar_border_color] => C0DEED 
       ) 

      [in_reply_to_user_id] => 
      [retweet_count] => 10 
      [in_reply_to_screen_name] => 
      [in_reply_to_status_id_str] => 
      [possibly_sensitive] => 
      [retweeted] => 
      [source] => WhoSay 
      [in_reply_to_user_id_str] => 
      [coordinates] => 
      [id] => 1.52240682072E+17 
      [text] => #Trollfoot Maroma http://t.co/4MvCYIFe 
     ) 

    [3] => stdClass Object 
     (
      [id_str] => 152240419344879616 
      [in_reply_to_status_id] => 
      [contributors] => 
      [place] => 
      [truncated] => 
      [geo] => 
      [favorited] => 
      [created_at] => Thu Dec 29 04:11:59 +0000 2011 
      [user] => stdClass Object 
       (
        [id_str] => 71876190 
        [default_profile] => 1 
        [contributors_enabled] => 
        [lang] => en 
        [protected] => 
        [url] => http://www.imdb.com/name/nm0000362/ 
        [profile_use_background_image] => 1 
        [name] => Danny DeVito 
        [default_profile_image] => 
        [friends_count] => 14 
        [profile_text_color] => 333333 
        [statuses_count] => 5361 
        [profile_background_image_url] => http://a0.twimg.com/images/themes/theme1/bg.png 
        [utc_offset] => -28800 
        [description] => I'm an actor, director and producer. 
        [is_translator] => 
        [created_at] => Sat Sep 05 19:44:48 +0000 2009 
        [profile_link_color] => 0084B4 
        [following] => 1 
        [profile_background_image_url_https] => https://si0.twimg.com/images/themes/theme1/bg.png 
        [favourites_count] => 8 
        [follow_request_sent] => 
        [geo_enabled] => 1 
        [profile_background_color] => C0DEED 
        [profile_background_tile] => 
        [followers_count] => 1784725 
        [profile_image_url] => http://a2.twimg.com/profile_images/400232403/Photo_6_normal.jpg 
        [screen_name] => DannyDeVito 
        [show_all_inline_media] => 
        [profile_sidebar_fill_color] => DDEEF6 
        [profile_image_url_https] => https://si0.twimg.com/profile_images/400232403/Photo_6_normal.jpg 
        [listed_count] => 20918 
        [notifications] => 
        [location] => Los Angeles 
        [id] => 71876190 
        [verified] => 1 
        [time_zone] => Pacific Time (US & Canada) 
        [profile_sidebar_border_color] => C0DEED 
       ) 

      [in_reply_to_user_id] => 
      [retweet_count] => 15 
      [in_reply_to_screen_name] => 
      [in_reply_to_status_id_str] => 
      [possibly_sensitive] => 
      [retweeted] => 
      [source] => WhoSay 
      [in_reply_to_user_id_str] => 
      [coordinates] => 
      [id] => 1.52240419345E+17 
      [text] => #Trolltoll Maroma http://t.co/Egv9syon 
     ) 
+0

Làm thế nào về cho chúng ta thấy đoạn code bạn đã thử? Chính xác _is_ "thông tin cụ thể mà bạn cần?" –

+0

chỉ cần nói ví dụ tôi chỉ muốn lấy id_str của từng đối tượng. – mcbeav

Trả lời

21

bạn không cần phải chuyển đổi chúng thành bất kỳ thứ gì.

foreach vòng lặp sẽ làm việc tốt với bạn như sau trong generic:

foreach ($objects as $obj) { 
    echo $obj->property; 
} 

cho một đối tượng bên trong này sẽ làm việc:

foreach ($objects as $obj){ 
     echo $obj->user->description; 
} 
+0

cảm ơn, bệnh cố gắng này ra, tôi chỉ không bao giờ thử nó và đã được ấn tượng rằng các đối tượng sẽ làm việc khác hơn so với mảng. – mcbeav

+0

làm việc như một sự quyến rũ, cảm ơn rất nhiều! – mcbeav

+7

Thực tế là ngược lại: 'foreach ($ objects as $ obj)' –

Các vấn đề liên quan