How count gets the length of a multidimensional array in PHP

Source: Internet
Author: User

The example in this paper describes how count gets the length of a multidimensional array in PHP. Share to everyone for your reference. The specific analysis is as follows:

Let's take a look at the following program run results:

$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 of the array in the multidimensional array is recursively computed (that is, 12 of your results). If you do not set mode defaults to 0. multidimensional arrays (arrays in arrays) are not instrumented (Result 3).

The first traversal is an array of outer arrays to derive two elements ("Color1″," Color2″, "Color3″") to 3
The iterated ("Color1″," Color2″, "Color3″") array is derived from 9 elements of 9
The result is 3+9=12.

Reference 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 you define an array in a different way, such as:
Copy the 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, the reference manual has the following paragraph:
Array:

The array in PHP is actually an ordered graph. A graph is a type that maps values to keys. This type is optimized in many ways, so you can use it as a real array, or list (vector), hash list (an implementation of the graph), dictionaries, collections, stacks, queues, and more possibilities. Because you can use another PHP array as a value, you can also easily emulate a tree.
Instance:
Obtaining the length of the first dimension of a two-dimensional or multidimensional array is a common procedure to judge, such as the array you are reading is a two-dimensional array:

<?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 has only two news, you want the number is also 2, but if you use the Count ($arr) different versions of PHP, the results of the statistics are not the same;
Later found in the PHP manual, 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 and does not detect multidimensional arrays (arrays in arrays);
1 (or count_recursive) for detecting multidimensional arrays,
So if you want to judge the reading of the array $arr is not a news message, it is necessary to write:

<?php
if (Is_array ($arr) && count ($arr, count_normal) >0)
{
.....
} else {
.....
}
?>

You can use this code to test the function:

<?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 ' statistics multidimensional array: '. Count ($arr, 1);//count ($arr, count_recursive)
?>

Well, at this point, we've solved the problem of getting the first-dimensional length of a two-dimensional or multidimensional array in PHP

How count gets the length of a multidimensional array in PHP

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.