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

How to get the length of an array in php

PHPz
PHPzOriginal
2023-04-18 14:12:15489browse

Array is a very common data type that can be used to store a series of data and can be accessed through an index or associated key. In PHP, we can use built-in functions or arithmetic operators to get the length of an array.

  1. count() function

count() function can return the number of elements in the array. Its syntax structure is as follows:

count(array $array, int $mode = COUNT_NORMAL) : int

Among them, $ array is the array whose length is to be calculated. $mode is an optional parameter used to specify the counting mode. It can take two values: COUNT_NORMAL and COUNT_RECURSIVE. The default value is COUNT_NORMAL, which means only counting the number of first-level elements of the array; if set If COUNT_RECURSIVE, the number of elements in all subarrays will be recursively calculated and the total number will be returned.

Sample code:

$array = ['apple', 'banana', 'cherry'];
$count = count($array);  // $count = 3
  1. sizeof() function

sizeof() function has basically the same function as count() function, it can return an array the number of elements in . The syntax structure is as follows:

sizeof(mixed $var, int $mode = COUNT_NORMAL) : int

Among them, $var represents the variable whose length is to be calculated, which can be any type of value, not limited to arrays; the meaning of $mode is the same as that of the count() function, which is used to specify Counting mode.

Sample code:

$array = ['apple', 'banana', 'cherry'];
$size = sizeof($array);  // $size = 3
  1. Arithmetic operators

The arithmetic operators in PHP can also be used to calculate the number of array elements, for indexed arrays , we can use the index of the last element plus 1 to calculate the array length; for associative arrays, we can use, first use the array_keys() function to get all the keys, and then use the count() function to calculate the number. Sample code:

// 索引数组
$array = ['apple', 'banana', 'cherry'];
$length = end($array) + 1;  // $length = 3

// 关联数组
$array = ['name' => 'Tom', 'age' => 18, 'gender' => 'male'];
$keys = array_keys($array);
$length = count($keys);  // $length = 3

Summary

There are three ways to express the length of an array in PHP: use the count() or sizeof() function, or use arithmetic operators to calculate. Each of these methods has its own advantages and disadvantages, and the most suitable method should be selected based on the application scenario and needs.

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!

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