How to obtain the length of a multi-dimensional array using count in php

Source: Internet
Author: User
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.

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.