In PHP, we often need to deal with arrays, and the length of the array (or the number of elements) is a very basic concept. So, how do we get the length of an array in PHP?
The answer is that PHP provides multiple methods to get the array length. This article will introduce four of the more commonly used methods, which are:
Next, we will introduce the usage and characteristics of these four methods in turn.
The count() function is a built-in function in PHP that returns the number of elements in an array. Its usage is very simple, just use the array to be counted as a parameter of the count() function. For example:
$my_array = array('apple', 'banana', 'orange'); $len = count($my_array); echo $len; // 输出 3
It should be noted that the count() function can only be used to count the number of elements in an array. If the parameter passed in is a non-array type, the count() function will return 1. Therefore, when using the count() function to count the length of an array, you need to first ensure that the parameter passed in is an array type.
The sizeof() function and the count() function have the same function, both are used to count the number of elements in the array. The difference is that the sizeof() function is an alias of the count() function, and there is no essential difference in use between the two. For example:
$my_array = array('apple', 'banana', 'orange'); $len = sizeof($my_array); echo $len; // 输出 3
Because the sizeof() function and the count() function are actually the same function, but they are written in different ways, so in actual development we can choose which one to use according to our own preferences and habits.
In addition to using the count() function and sizeof() function, we can also use the foreach loop to get the length of the array. The specific method is to traverse the array and keep a counter accumulating until the last element is traversed. For example:
$my_array = array('apple', 'banana', 'orange'); $len = 0; foreach ($my_array as $value) { $len++; } echo $len; // 输出 3
It should be noted that when using the foreach loop to traverse the array, we only need to traverse the values in the array without using the keys of the array. Therefore, each element needs to be assigned to a variable, here we choose $value.
array_keys() function is a built-in function in PHP that returns all the key names of an array. If we pass an array of length n as a parameter to the array_keys() function, the function will return an array containing n elements, and the elements are all the key names in the original array. Therefore, we can use this method to calculate the length of an array. For example:
$my_array = array('apple', 'banana', 'orange'); $keys = array_keys($my_array); $len = count($keys); echo $len; // 输出 3
It should be noted that the method of calculating the array length using the array_keys() function is slightly more complicated than other methods. However, if we need to get the key name and length of the array at the same time, using the array_keys() function is a good choice.
Summary
The above introduces 4 methods commonly used in PHP to obtain the length of an array. If you only need to get the length of the array without getting the key names at the same time, it is simpler to use the count() function or the sizeof() function, while using the foreach loop is a more flexible and versatile method. If you need to get the key name and length at the same time, using the array_keys() function is a good choice.
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!