zoukankan      html  css  js  c++  java
  • php count()函数使用详解

    count

    (PHP 4, PHP 5)

    count — Count all elements in an array, or something in an object

     

     

    int count ( mixed $var [, int $mode = COUNT_NORMAL ] )

    Counts all elements in an array, or something in an object.

    For objects, if you have SPL installed, you can hook into count() by implementing interface Countable. The interface has exactly one method,Countable::count(), which returns the return value for the count() function.

    Please see the Array section of the manual for a detailed explanation of how arrays are implemented and used in PHP.

     

    var

    The array or the object.

    mode

    If the optional mode parameter is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. This is particularly useful for counting all the elements of a multidimensional array. count() does not detect infinite recursion.

    reject note Return Values

    Returns the number of elements in var. If var is not an array or an object with implemented Countable interface, 1 will be returned. There is one exception, if var is NULL0 will be returned.

     

    Example #1 count() example

    <?php
    $a[0] = 1;
    $a[1] = 3;
    $a[2] = 5;
    $result = count($a);
    // $result == 3

    $b[0]  = 7;
    $b[5]  = 9;
    $b[10] = 11;
    $result = count($b);
    // $result == 3

    $result = count(null);
    // $result == 0

    $result = count(false);
    // $result == 1
    ?>

    Example #2 Recursive count() example

    <?php
    $food = array('fruits' => array('orange', 'banana', 'apple'),
                  'veggie' => array('carrot', 'collard', 'pea'));

    // recursive count
    echo count($food, COUNT_RECURSIVE); // output 8

    // normal count
    echo count($food); // output 2

    ?>

    从上面的代码可以看出,count如果设置为1计算的是多维数组所有维度的元素个数。上面的二维数组第1维有2个元素,每维又有3

    个元素。所有加起来是8个。

    有时候我们希望可以限定维度,下面的函数可以做到:

    <?php 
      function getArrCount ($arr, $depth=1) { 
          if (!is_array($arr) || !$depth) return 0; 
             
         $res=count($arr); 
             
          foreach ($arr as $in_ar) 
             $res+=getArrCount($in_ar, $depth-1); 
          
          return $res; 
      } 
    ?>
    getArrCount($food,2)还是8,我们可以自由第传入深度。
    很多时候我们不想去计算所有维度的元素个数,我们只想计算总共到底有多少个元素。采用下面的这个函数可以做到。

    // $limit is set to the number of recursions 

    function count_recursive ($array, $limit) { 
        $count = 0; 
        foreach ($array as $id => $_array) { 
            if (is_array ($_array) && $limit > 0) { 
                $count += count_recursive ($_array, $limit - 1); 
            } else { 
                $count += 1; 
            } 
        } 
        return $count; 

    count_recursive($food,1) 输出为6. 注意我们传入的limit为维度-1,而第一个则是维度。我们要想传入数组的维度,可以

    在$count=0前加一句$limit=$limit-1;

    这样,传入的是多少,就是求几维的个数。


    对下面的3维数组。
     
    $arr=array("key1"=>array("key2"=>array(1,2,3),4,5),
                "key2"=>array("key3"=>array(7,8,9),10,11) );

    getArrCount($arr,2)为8.

    count_recursive ($arr,2); 为10.

    count_recursive($arr,3)也为10.为什么?

    注意上面的count_recrusive是维度减1.是三维的最大limt为2,。

     PHP判断一个二维数组是否为空的方法,直接用empty()判断是不行的,如下方法:
    $temp = array(array(), array());
    如果一个数组中包含很多空的数组,那么直接用empty()判断是不行的。你可以这样判断
    $temp = array_filter($temp) ; array_filter($arr,callback),如果没有提供 callback 函数, 将删除 input 中所有等值为 FALSE 的条目。由于空数组为False,所以删除了。
    if(!empty($temp))
    这样就会把 $temp 中空的数组剔除了。

  • 相关阅读:
    Button 使用Command 按钮置灰未更新
    C# TextBox 焦点
    MultiTigger 绑定异常处理
    C# 获取程序路径
    Linux 权限设置chmod
    WPF SpreadSheetGear电子表单
    WPF 窗口
    Excel公式 提取文件路径后缀
    C#/VB.NET 获取电脑属性(硬盘ID、硬盘容量、Cpu序列号、MAC地址、系统类型)
    DevExpress Carousel 设置水平滑动列表
  • 原文地址:https://www.cnblogs.com/youxin/p/2329405.html
Copyright © 2011-2022 走看看