This article illustrates the implementation of count in PHP to get the length of multidimensional arrays. Share to everyone for your reference. The specific analysis is as follows:
Let's take a look at the following program run results:
Copy Code code as follows:
$numb =array (
Array (10,15,30), Array (10,15,30), Array (10,15,30)
);
echo Count ($numb, 1);
A.3
b.12
C.6
D.9
The answer is B.
If mode is set to Count_recursive (or 1) in the COUNT function, the number of elements in the array in the multidimensional array is recursively computed (that is, 12 of your result). If you do not set mode to default to 0. does not detect multidimensional arrays (arrays in an array) (Result 3).
The first traversal is an array of outer arrays that has two elements ("Color1″," Color2″, "Color3″") for 3
The iterated ("Color1″," Color2″, "Color3″" array) is 9 elements 9
The result is 3+9=12.
Reference Example:
Copy Code code as follows:
<?php
$fruits = Array (
Array (1, 2,null,null, 5, 6),
Array (1, 2,null,null, 5, 6),
);
Echo (count ($fruits [0]));
?>
If you define an array in a different way, such as:
Copy Code code as follows:
<?php
$fruits [0][0]=1;
$fruits [0][3]=1;
$fruits [0][4]=1;
Echo (count ($fruits [0]));
?>
This will output 3, because the array in PHP does not require the index to be contiguous, and the reference manual has the following paragraph:
Array:
the array in PHP is actually an ordered graph. A diagram is a type that maps values to the keys. This type is optimized in many ways, so you can use it as a real array, or a list (vector), a hash list (an implementation of the graph), a dictionary, a collection, a stack, a queue, and more possibilities. Because you can use another PHP array as a value, you can also easily simulate a tree.
Instance:
To get the length of the first dimension of a two-dimensional or multidimensional array, this is a commonly used program judgment, such as the array you read is a two-dimensional array:
Copy Code code as follows:
<?php
$arr =array (
0=>array (' title ' => ' News 1 ', ' Viewnum ' => 123, ' content ' => ' ZAQXSWEDCRFV '),
1=>array (' title ' => ' News 2 ', ' Viewnum ' =>, ' content ' => ' qwertyuiopzxcvbnm ')
);
?>
If you want to count the length of the array $arr, which means that the two-dimensional array is only two news, the number you want is 2, but if you use the different versions of PHP with Count ($arr), the results are not the same;
Later found in the PHP manual, the Count function also has a second argument, which is explained as follows:
The Count function has two parameters:
0 (or Count_normal) is the default and does not detect multidimensional arrays (arrays in an array);
1 (or count_recursive) to detect multidimensional arrays,
So if you want to determine whether the read array $arr has news information, it should be written:
Copy Code code as follows:
<?php
if (Is_array ($arr) && count ($arr, count_normal) >0)
{
.....
} else {
.....
}
?>
You can use this code to test the function:
Copy Code code as follows:
<?php
$arr =array (
0=>array (' title ' => ' News 1 ', ' Viewnum ' => 123, ' content ' => ' ZAQXSWEDCRFV '),
1=>array (' title ' => ' News 2 ', ' Viewnum ' =>, ' content ' => ' qwertyuiopzxcvbnm ')
);
Echo ' does not count multidimensional arrays: '. Count ($arr, 0);//count ($arr, Count_normal)
echo "<br/>";
Echo ' statistical multidimensional array: '. Count ($arr, 1);//count ($arr, count_recursive)
?>
Well, to this point, you've solved the problem of getting the first dimensional length of a two-dimensional or multidimensional array in PHP.
I hope this article will help you with your PHP program design.