How to get the length of an array in php

PHPz
Release: 2023-04-19 09:42:58
Original
1889 people have browsed it

Array is a very important data structure when programming in PHP. When we need to operate an array, we usually need to get the length of the array, that is, the number of elements it contains. This article will introduce several methods to obtain the length of a PHP array.

Method 1: count function

The count function is PHP’s own array length calculation function, which can return the number of elements of an array. The following is the syntax of this function:

count($array,COUNT_NORMAL);
Copy after login

Among them, the first parameter $array represents the array that needs to be calculated, and the second parameter COUNT_NORMAL represents using the default mode for calculation. In default mode, the count function recursively counts the number of elements in a multidimensional array.

You can easily get the length of an array using the count function. The following is a sample program:

Copy after login

Output result:

4
Copy after login
Copy after login
Copy after login
Copy after login

Method 2: sizeof function

The sizeof function has a similar function to the count function and can be used to calculate the length of the array . The following is the syntax of this function:

sizeof($array);
Copy after login

Among them, the parameter $array represents the array whose length needs to be calculated, which can be a one-dimensional or multi-dimensional array.

Use the sizeof function to get the array length. The following is a sample program:

Copy after login

Output result:

4
Copy after login
Copy after login
Copy after login
Copy after login

Method 3: Array Iterator

In PHP5 and above, you can use the array iterator (ArrayIterator) to Iterate over an array and get its length. The following is a sample program using an array iterator:

count();
?>
Copy after login

Output result:

4
Copy after login
Copy after login
Copy after login
Copy after login

Method 4: Use a loop to calculate the length

Another way to get the length of an array is to use Loop through the array and count the number of elements. The following is a sample program:

Copy after login

Output result:

4
Copy after login
Copy after login
Copy after login
Copy after login

The above are several methods of obtaining the length of a PHP array. Readers can choose to use different methods according to actual needs. It should be noted that when the number of arrays is large, it may be time-consuming to use a loop to calculate the length. It is recommended to use the count or sizeof function.

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

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!