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
    )
4

1 に答える 1

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.. */ }
}

おそらく、問題のより良い説明が役立つかもしれません。

于 2012-05-26T13:04:54.800 に答える