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

How to get the array length in php

PHPz
PHPzOriginal
2023-04-20 10:11:12503browse

PHP is a server-side programming language often used to build web applications. When writing web applications, we usually need to deal with various data types, the most common of which is arrays. In PHP, we can use various built-in functions to manipulate arrays, such as getting the array length. This article will introduce how to get the length of an array in PHP.

  1. count() function

In PHP, the easiest way is to use the built-in function count() to get the length of an array. The count() function accepts an array as a parameter and returns the number of elements in the array. For example:

$array = array('apple', 'banana', 'orange');
$count = count($array);
echo $count;

Running the above code will output:

3
  1. sizeof() function

is similar to the count() function, and the sizeof() function also Can be used to get the length of an array. The sizeof() function has the same functionality as the count() function, but may be faster in some cases. For example:

$array = array('apple', 'banana', 'orange');
$size = sizeof($array);
echo $size;

Running the above code will also output:

3

Note: In actual use, the usage of count() and sizeof() functions are basically the same, the main difference lies in the return value of the function type. The return type of the count() function is an integer, and the return type of the sizeof() function is an integer or other integer type (such as long, short, etc.).

  1. foreach loop

We can also use the foreach loop to traverse the array and calculate its length. For example:

$array = array('apple', 'banana', 'orange');
$length = 0;
foreach ($array as $value) {
    $length++;
}
echo $length;

Running the above code will get the same output as the first two methods:

3
  1. array_key_last() function

in PHP 7.3 In versions above, we can use the array_key_last() function to get the key name of the last element in the array. Because the array keys are numbers, we can calculate the array length by this key. For example:

$array = array('apple', 'banana', 'orange');
$length = array_key_last($array) + 1;
echo $length;

Running the above code will get the same output as the first two methods:

3

Summary

This article introduces four methods to get the length of an array in PHP : count() function, sizeof() function, foreach loop and array_key_last() function. Among them, the first three methods can be used in various versions of PHP; the array_key_last() function can only be used in PHP 7.3 and above. Therefore, we can choose different methods to obtain the array length according to actual needs. Either way, we can easily get the array length in PHP code and process it accordingly.

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