Home  >  Article  >  Backend Development  >  How to get the array length in php

How to get the array length in php

PHPz
PHPzOriginal
2023-04-25 17:37:411854browse

In PHP, we usually need to deal with some arrays. When using arrays, we often need to know the length of the array in order to perform corresponding operations. So, how to get the length of a PHP array?

In PHP, we can use the count() function to get the length of an array. Its syntax is as follows:

count(array,mode)

Among them, array represents the array to be counted, and mode is an optional parameter used to specify the counting method.

If the mode parameter is not passed in, the function will use the COUNT_NORMAL mode for counting by default. In this mode, the function returns the length of the array, which is the number of elements in the array.

For example, in the following sample code, we first create an array containing 5 elements, and then use the count() function to get the length of the array and output the result:

// 定义数组
$arr = array(1, 2, 3, 4, 5);

// 获取数组长度
$length = count($arr);

// 输出结果
echo $length;  // 输出:5

If you want to use the function To specify the counting method when using it, you can pass in the mode parameter. Currently, PHP provides two counting methods:

  • COUNT_NORMAL: Default method, returns the number of elements in the array
  • COUNT_RECURSIVE: Recursive counting, returns the number of all elements in the array, and Expand a multi-dimensional array into a one-dimensional array for counting

For example, in the following sample code, we create an array containing multi-dimensional arrays, then use the count() function and specify the COUNT_RECURSIVE mode count, and finally output Result:

// 定义多维数组
$arr = array(
    1 => array(1, 2, 3),
    2 => array(4, 5),
    3 => array(
            6,
            array(7, 8),
            9
        )
);

// 指定 COUNT_RECURSIVE 模式计数
$length = count($arr, COUNT_RECURSIVE);

// 输出结果
echo $length;  // 输出:10

In addition to using the count() function, we can also use the PHP built-in function sizeof() to get the array length. Its syntax is exactly the same as the count() function, as shown below:

sizeof(array,mode)

Therefore, the above example code can also be implemented using the sizeof() function, as shown below:

// 获取数组长度
$length = sizeof($arr, COUNT_RECURSIVE);

// 输出结果
echo $length;  // 输出:10

In summary , The method of obtaining the length of an array in PHP is very simple, we can use the count() or sizeof() function to achieve it. By passing different parameters, we can flexibly calculate the length of multi-dimensional arrays and implement other functions. Therefore, when developing PHP programs, we should be proficient in these methods and use them reasonably when needed.

The above is the detailed content of How to get the array length in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn