duanliangman5398 2013-07-13 16:02
浏览 39

计算PHP数组中的出现次数

I'm playing around with opencart and trying to add a few new features to the filters functionality of the cart.

I'm wondering how do I efficiently count the number of occurrences of a given value in a multidimensional php array? For example, I'm trying to work our if all Filter->Count == 0?

Array
    (
    ...
    [filter] => Array
       (
       [0] => Array
       (
          [filter_id] => 109
          [name] => Boyfriends (0)
          [count] => 0
       )

       [1] => Array
       (
          [filter_id] => 114
          [name] => Daughters (0)
          [count] => 0
       )

       [2] => Array
       (
          [filter_id] => 115
          [name] => Fathers (0)
          [count] => 0
       )

       [3] => Array
       (
          [filter_id] => 108
          [name] => For Her (53)
          [count] => 53
       )
    ...
    )
...
)
  • 写回答

1条回答 默认 最新

  • dongzhuang5741 2013-07-13 16:35
    关注
    $amount = 0;
    foreach($array as $value) {
        if($value['count'] == 0)
            $amount++;
    }
    

    That would return the amount of 'counts' that equal to zero. To return the amount that aren't 0, use this:

    $amount = 0;
    foreach($array as $value) {
        if($value['count'] != 0)
            $amount++;
    }
    

    Then $amount will equal zero iff all the 'count' values are zero.

    评论

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效