dtz55359 2012-05-26 12:45
浏览 6
已采纳

如何按所使用的数组顺序显示哪个元素?

This questions seems like it already ought to be answered here, however I can't find it, probably due to lack of terminology. Hopefully someone else in the same situation could benfit from this.

I have an array $post_item.(se below) I print data from it for example

$post_item->ID

Is there a way to get the which item in the order I am printing. In other words I would like to count the items.

If the item is the one with ID 9 I would like to return a 0. If it's the one with ID 12, I would like to return a 1 etc.

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
    )
  • 写回答

1条回答 默认 最新

  • dongsu3654 2012-05-26 13:04
    关注

    just convert to array if you want "keys" (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.. */ }
    }
    

    perhaps a better explanation of the problem could help.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 组策略中的计算机配置策略无法下发
  • ¥15 如何绘制动力学系统的相图
  • ¥15 对接wps接口实现获取元数据
  • ¥20 给自己本科IT专业毕业的妹m找个实习工作
  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)