In-depth understanding of the count function in PHP

Reprinted from   http://www.jb51.net/article/85525.htm

In PHP programming, when traversing an array, it is often necessary to first calculate the length of the array as the judgment condition for the end of the loop. In PHP, the operation of the array is very frequent, so count is also a commonly used function. Let's study the count function. specific implementation.

count

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

The count function counts all elements in an array or object.

For objects, if you have the SPL extension installed, you can call the count function by implementing the Countable interface. The Countable interface has one and only one method, Countable::count(), which returns the return value of the count() function.

Parameter Description

mode

If the parameter mode is set to COUNT_RECURSIVE (or 1), count() will recursively count the array. Especially useful when computing multidimensional arrays.

The count function will return 1 if the first argument is not an array or an object that implements the Countable interface.

Note: the count function can detect recursion to avoid infinite loops, but will return E_WARNING when encountering infinite recursion or getting a larger than expected value.

Run the example

Common application

$arr1 = array(1, 2, 3, 4, 5);
$val1 = count($arr1); // 5

Multidimensional Arrays

$arr2 = array('apple', 'banana', array('cat', 'camel'), 'dog');
$val2_1 = count($arr2); // 4
$val2_2 = count($arr2, 1); // 6

 numbers and strings

$str = "hello world";
$int_val = 1;
$val3 = count($str); // 1
$val4 = count($int_val); // 1

普通对象

class User {
  private $name;
  private $address;
}

$user = new User();
$val5 = count($user); // 1
$val6 = count((array) $user); // 2

array-like对象

class User extends ArrayObject {
    private $name;

    public function __construct() {
      $this->name = 'hhq';
    }

    public function getName() {
      return $this->name;
    }

    public function count() {
      return 2;
    }

  }

  $user2 = new User();
  $val7 = count($user2); // 2

实现Countable接口对象

class User implements Countable {
    public function count() {
      return 3;
    }
  }

  $user3 = new User();
  $val8 = count($user3); // 3

运行步骤

进入switch语句检测参数类型

如果是NULL,直接返回0

如果是数组,调用php_count_recursive函数机选数组元素个数

如果是对象,先检查是否为数组对象(array-like object),如果是,则计算数组对象的数量

否则,如果对象实现了Countable接口,则调用Countable的count方法

最后,其他类型比如整型数组或字符串,都返回1。

源码解读

如果是普通数组,count函数会调用php_count_recursive函数实现其功能的运行步骤如下:

如果当前hash Bucket被递归访问的次数大于1,说明重复递归,染回E_WARNING错误

否则计算当前数组层数的数组元素个数

如果有递归参数选项,则继续递归访问

如果参数是对象类型,实现时会先判断handler是否被定义。而handler是PHP内核中对象的结构体,其中包含有count_elements字段,实际上是一个函数。如果某个对象表现得想数组一样,即通常说的array-like object,那么就会执行count_elements函数。具体实现是类继承PHP的ArrayObject,并在类里面实现count函数,具体调用的就是count函数,如果类没有实现count函数,则count返回0,否则返回对象的count函数的返回值。

如果是其他的数据类型

1、字符串

2、数字

3、对象分支中两个if判断都为false的情况,即没有继承ArrayObject且没有实现Countable接口。

这些类型通通返回1。

需要注意的是,如果需要计算的是对象的属性数量,可以先将对象转换成数组,然后调用count函数。如:

$count_value = count((array) $user);

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324670614&siteId=291194637