2012-05-26 40 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