This article mainly introduces how to obtain the length of multi-dimensional array by count in php. The example analyzes the principle of array and summarizes the calculation method of array length, which is of great practical value, for more information about how to use count in php to obtain the length of a multi-dimensional array, see the following example. Share it with you for your reference. The specific analysis is as follows:
Let's take a look at the following program running results:
The Code is 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 the mode in the count function is set to COUNT_RECURSIVE (OR 1), the number of elements in the array in the multi-dimensional array (that is, the 12 in your result) is calculated recursively ). If the mode is not set, the default value is 0. Multi-dimensional arrays (arrays in arrays) are not detected (result 3 ).
First, we traverse the outer array to get two elements ("color1", "color2", "color3") 3.
Traverse the Array ("color1", "color2", "color3") to get 9 Elements
The result is 3 + 9 = 12.
Example:
The Code is as follows:
<? Php
$ Fruits = array (
Array (1, 2, null, null, 5, 6 ),
Array (1, 2, null, null, 5, 6 ),
);
Echo (count ($ fruits [0]);
?>
If an array is defined in another way, for example:
The Code is as follows:
<? Php
$ Fruits [0] [0] = 1;
$ Fruits [0] [3] = 1;
$ Fruits [0] [4] = 1;
Echo (count ($ fruits [0]);
?>
In this case, 3 is output, because arrays in php do not require the index to be continuous. The reference manual contains the following section:
Array:
Arrays in PHP are actually an ordered graph. A graph is a type that maps values to keys. This type has been optimized in many ways, so you can use it as a real array, or a list (vector), a hash (an implementation of a graph), a dictionary, set, stack, queue, and more possibilities. Because another PHP array can be used as the value, and the tree can be easily simulated.
Instance:
Obtain the length of the first dimension of a two-dimensional or multi-dimensional array. This is a common program judgment. For example, the array you read is a two-dimensional array:
The Code is as follows:
<? Php
$ Arr = array (
0 => array ('title' => 'news 1', 'viewnum' => 123, 'content' => 'zaqxswedcrfv '),
1 => array ('title' => 'news 2', 'viewnum' => 99, 'content' => 'qwertyuiopzxcvbnm ')
);
?>
If you want to count the length of the array $ arr, that is to say, the two-dimensional array has only two pieces of news, and the number you want is 2, but if you use different versions of php in count ($ arr, the statistical results are different;
Later, I found in the php manual that the count function has a second parameter, which is explained as follows:
The count function has two parameters:
0 (or COUNT_NORMAL) is the default value. Multi-dimensional arrays (arrays in the array) are not detected );
1 (or COUNT_RECURSIVE) is a multi-dimensional array for detection,
Therefore, if you want to determine whether the read array $ arr has news information, write the following statement:
The Code is as follows:
<? Php
If (is_array ($ arr) & count ($ arr, COUNT_NORMAL)> 0)
{
.....
} Else {
.....
}
?>
You can use this code to test the function:
The Code is as follows:
<? Php
$ Arr = array (
0 => array ('title' => 'news 1', 'viewnum' => 123, 'content' => 'zaqxswedcrfv '),
1 => array ('title' => 'news 2', 'viewnum' => 99, 'content' => 'qwertyuiopzxcvbnm ')
);
Echo 'multi-dimensional array not counted:'. count ($ arr, 0); // count ($ arr, COUNT_NORMAL)
Echo"
";
Echo 'multi-Dimensional Statistics array: '. count ($ arr, 1); // count ($ arr, COUNT_RECURSIVE)
?>
Now, the first-dimensional length of a two-dimensional or multi-dimensional array in php has been solved.
I hope this article will help you with PHP programming.