2012-05-26 41 views
0

這個問題似乎已經應該在這裏得到解答,但是我找不到它,可能是因爲缺乏術語。希望在相同情況下的其他人可以從中受益。如何顯示使用的數組順序中的元素?

我有一個數組$ post_item。(參見下文),我從它打印數據例如

$post_item->ID 

有沒有一種辦法讓我打印定單的哪個項目。換句話說,我想統計這些項目。

如果該項目是與ID 9所述一個我想返回0。如果它是一個與ID 12,我想返回1等

stdClass Object 
    (
     [ID] => 9 
     [post_author] => 1 
     [post_date] => 2012-05-18 12:01:49 
     [post_date_gmt] => 2012-05-18 12:01:49 
     [post_content] => 
     [post_title] => barnbildarkiv 
     [post_excerpt] => 
     [post_status] => inherit 
     [comment_status] => open 
     [ping_status] => open 
     [post_password] => 
     [post_name] => barnbildarkiv 
     [to_ping] => 
     [pinged] => 
     [post_modified] => 2012-05-18 12:01:49 
     [post_modified_gmt] => 2012-05-18 12:01:49 
     [post_content_filtered] => 
     [post_parent] => 0 
     [guid] => http://dev.nuagency.se/wp-content/uploads/2012/05/barnbildarkiv.jpg 
     [menu_order] => 0 
     [post_type] => attachment 
     [post_mime_type] => image/jpeg 
     [comment_count] => 0 
     [filter] => raw 
    ) 
    stdClass Object 
    (
     [ID] => 12 
     [post_author] => 1 
     [post_date] => 2012-05-18 12:02:00 
     [post_date_gmt] => 2012-05-18 12:02:00 
     [post_content] => 
     [post_title] => animals_hm_giftcards 
     [post_excerpt] => 
     [post_status] => inherit 
     [comment_status] => open 
     [ping_status] => open 
     [post_password] => 
     [post_name] => animals_hm_giftcards 
     [to_ping] => 
     [pinged] => 
     [post_modified] => 2012-05-18 12:02:00 
     [post_modified_gmt] => 2012-05-18 12:02:00 
     [post_content_filtered] => 
     [post_parent] => 0 
     [guid] => http://dev.nuagency.se/wp-content/uploads/2012/05/animals_hm_giftcards.jpg 
     [menu_order] => 0 
     [post_type] => attachment 
     [post_mime_type] => image/jpeg 
     [comment_count] => 0 
     [filter] => raw 
    ) 
    stdClass Object 
    (
     [ID] => 63 
     [post_author] => 1 
     [post_date] => 2012-05-21 09:27:41 
     [post_date_gmt] => 2012-05-21 09:27:41 
     [post_content] => 
     [post_title] => wren-2 
     [post_excerpt] => 
     [post_status] => inherit 
     [comment_status] => open 
     [ping_status] => open 
     [post_password] => 
     [post_name] => wren-2 
     [to_ping] => 
     [pinged] => 
     [post_modified] => 2012-05-21 09:27:41 
     [post_modified_gmt] => 2012-05-21 09:27:41 
     [post_content_filtered] => 
     [post_parent] => 62 
     [guid] => http://dev.nuagency.se/wp-content/uploads/2012/05/wren-2.gif 
     [menu_order] => 0 
     [post_type] => attachment 
     [post_mime_type] => image/gif 
     [comment_count] => 0 
     [filter] => raw 
    ) 
+0

這些不是數組,它們是類stdClass的對象。如果你有幾個,爲什麼不把它們全部放到一個數組上? '$ posts [] = $ postItem;'然後你自動得到他們的索引0,1,2。 –

+0

writing $ posts [] = $ post_item; echo「post_item

"; print_r($posts); echo "
」;給我一個陣列看起來像post_item 陣列 ( [0] => stdClass的對象 ( [ID] => 9 ... ) – Himmators

回答

0

剛剛轉換成數組,如果你想「鑰匙」(0,1,2 ..)

$arr = array(); 
foreach($objects as $key=>$val) { $arr[$key] = $val; } 

foreach($arr as $key=>$val) { 
    if ($val["ID"] == 9) { /* $key is 0,1,2.. */ } 
} 

也許更好的解釋問題可能會有所幫助。

+0

這只是一個例子,我總是希望第一個爲0第二個爲1,第三個爲3等,不管身份證。 – Himmators

+1

檢查編輯後 – 2012-05-26 14:08:12