Implementation of count multi-dimensional array length statistics in php

Source: Internet
Author: User
For more information about how to implement count multi-dimensional array length statistics in php, see limit. The following program runs the result as () Code: Copy the code below? $ Numbarray (n...

For more information about how to implement count multi-dimensional array length statistics in php, see limit.

The program running result below is ()

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

Answer: 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, traverse the array outside to get two elements ("color1", "color2", "color3") 3
Traverse the array ("color1", "color2", "color3") to get 9 elements
The result is 3 + 9 = 12.

Reference

The code is as follows:

$ Fruits = array (
Array (1, 2, null, null, 5, 6 ),
Array (1, 2, null, null, 5, 6 ),
);

Echo (count ($ fruits [0]);
?>

You may be talking about arrays defined in other ways, such as directly using:

The code is as follows:

$ 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:
$ 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:
If (is_array ($ arr) & count ($ arr, COUNT_NORMAL)> 0)
{
.....
} Else {
.....
}
?>

You can use this code to test the function:

The code is as follows:
$ 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.



Article link:

Save this article for favorites!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.