douxie1692 2010-05-03 22:04
浏览 27
已采纳

php数组计数

I have a var dump of my sql query which return the following

I wanna to count in the array below that how many rows of myID = 5 are there. How would I do that. I am using php. Thanks in advance

 array
  0 => 
    object(stdClass)[17]

      public 'myID' => string '5' (length=1)
      public 'data' => string '123' (length=3)
  1 => 
    object(stdClass)[18]

      public 'myID' => string '5' (length=1)
      public 'data' => string '123' (length=3)
  2 => 
    object(stdClass)[19]

      public 'relativeTypeID' => string '2' (length=1)
      public 'data' => string '256' (length=3)
  3 => 
    object(stdClass)[20]

      public 'myID' => string '4' (length=1)
      public 'data' => string '786' (length=3)

    object(stdClass)[21]

      public 'myID' => string '4' (length=1)
      public 'data' => string '786' (length=3)
  • 写回答

2条回答 默认 最新

  • douci2516 2010-05-03 22:08
    关注

    Do you always have the same value of data for the same myID? In other words, is data functionally dependant on myID?

    If so, you can get the database to do this for you:

    SELECT myID, data, COUNT(*) AS cnt
    FROM (your query here)
    GROUP BY myID, data
    

    This would give you results like the following:

    myID  data   cnt
    '5'   '123'  3
    '2'   '256'  1
    '4'   '786'  2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度