PHP method of obtaining the length of an array
1. Method of obtaining the length of a one-dimensional array
1, count, and sizeof can directly count the length of one-dimensional arrays.
2. For example:
$arr = Array('0','1','2','3','4'); echo count($arr); // 输出5 $arr = array('A','B','C'); echo sizeof($arr); // 输出3
3. sizeof() and count() have the same purpose. Both functions can return the number of array elements. You can get the number of elements in a regular scalar variable. If the array passed to this function is an empty array or a variable that has not been set, the number of array elements returned is 0; the function is the same.
2. The difference and connection between the sizeof method and the count method
sizeof() is an alias of the function count().
3. Get the length of the multi-dimensional array
1. For example:
a,
$arr = array( array( 'username' => 'zhangsan', 'password' => '123456'), array( 'username' => 'zenghu', 'password' => '123456' ) );
b. If you want to count $ The length of arr, that is to say, you want to count two users, the number is 2;
c. But if you use count($arr) with different versions of PHP, the statistical results will be different;
d. Later, I found in the PHP manual that the count function has a second parameter, which is explained as follows:
e. The count function has two parameters:
0 (or COUNT_NORMAL) is By default, multi-dimensional arrays (arrays within arrays) are not detected;
1 (or COUNT_RECURSIVE) is used to detect multi-dimensional arrays;
2, the length of statistical arrays:
a, statistical two-dimensional The length:
count($arr, 1);
b. Not counting the length of the two-dimensional array:
count($arr, 0);
c. Determining whether there is user information:
if( is_array($arr) && count($arr, COUNT_NORMAL)>0 ){ // 有; }else{ // 没有; }
Recommended tutorial: PHP Video tutorial
The above is the detailed content of How to express the length of php array. For more information, please follow other related articles on the PHP Chinese website!