How to determine the length of an array in php

PHPz
Release: 2023-05-07 15:36:08
Original
795 people have browsed it

In PHP, array is a very common and useful data type. When we deal with arrays, sometimes we need to know the length of the array in order to perform the next operation. This article will introduce several methods to determine the length of a PHP array.

  1. count() function

PHP’s built-in count() function can be used to get the number of elements in an array. This function can receive an array as a parameter and returns the number of elements in the array.

Here is an example:

$arr = array('apple', 'banana', 'cherry');
$length = count($arr);
echo $length; // 输出 3
Copy after login

In the above code, we calculated the length of the array $arr using the count() function and stored the result in the variable $length.

It should be noted that the count() function can also be used to count the number of elements of other types, such as strings, objects, and resources. If the function is passed to something other than an array, 1 will be returned.

  1. sizeof() function

The sizeof() function in PHP is similar to the count() function and can be used to obtain the number of elements in an array. This function can receive an array as a parameter and returns the number of elements in the array. Compared with the count() function, the functions of the sizeof() function are almost identical.

Here is an example:

$arr = array('apple', 'banana', 'cherry');
$length = sizeof($arr);
echo $length; // 输出 3
Copy after login

In the above code, we use the sizeof() function to calculate the length of the array $arr and store the result in the variable $length.

It should be noted that the sizeof() function is almost the same as the count() function in terms of functionality, but in terms of performance, the count() function is slightly faster.

  1. Built-in variable $length

There is also a built-in variable $length in PHP, which can be used to get the length of the array. This variable directly stores the length of the array, so using it can get the length of the array faster.

It should be noted that the $length variable is only available in PHP 7.2 or above.

Here is an example:

$arr = array('apple', 'banana', 'cherry');
$length = $arr.length;
echo $length; // 输出 3
Copy after login

In the above code, we use the built-in variable $arr.length to get the length of the array and store the result in the variable $length.

It should be noted that the $length variable can only be used for arrays, strings and objects.

  1. Array functions

There are many other array functions in PHP that can be used to obtain the length of an array or other related information. For example, the array_key_exists() function can be used to determine whether a key exists in an array, the array_values() function can return all the values ​​in an array, and so on.

The following is an example:

$arr = array('apple', 'banana', 'cherry');
$length = array_key_exists(2, $arr);
echo $length; // 输出 true
Copy after login

In the above code, we use the array_key_exists() function to determine whether key 2 exists in the array $arr, and store the result in the variable $length .

The above are several ways to obtain the length of a PHP array. You can choose the method that best suits you according to the situation. Either way, it will help you handle PHP arrays easily.

The above is the detailed content of How to determine 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