How to get the length of php array

藏色散人
Release: 2023-04-04 13:58:02
Original
7264 people have browsed it



We can add elements to the array or remove elements from the array, so if we want to know the presence in the array The total length or total number of elements, we can use the count() or sizeof function.

How to get the length of php array

Let’s use a simple example to introduce how to get the length of an array in PHP.

Method 1: count() function

First we create an array, as follows:

$array = array  ("ABC","DEF", "GHI","KLM");
Copy after login

Then we use the count function to Prints the number of elements in this array.

print count($array);
Copy after login

Output result:

Output 4 as shown in the figure, because there are four elements in the above array.

Note: The count() function counts the number of cells in the array or the number of attributes in the object.

Method 2: sizeof() function

We can also use the sizeof function to determine the number of values.

Code example:

$value= array(2,5,6,8,9);
echo "数组值的数量是".sizeof($value)."<br>";
Copy after login

The output result is as follows:

How to get the length of php array

The above calculation output is 5.

Note: The sizeof() function calculates the number of cells in the array or the number of attributes in the object. The sizeof() function is an alias of the count() function.

This article is an introduction to two methods of obtaining the array length in PHP. It is very simple and easy to understand. I hope it will be helpful to friends in need!



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

Related labels:
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