Example of how count gets a multidimensional array length in PHP

Source: Internet
Author: User
In the previous article we introduced the PHP array length to get the method, but the array is divided into one-dimensional array, multidimensional array, then this article describes the PHP count to get a multidimensional array length implementation. 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:

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

Summarize:

This article has been solved in PHP to get a two-dimensional or multidimensional array of the first dimension of the problem, to get the length of the PHP array is not very simple, I hope you have some help!

Related recommendations:

How to get the length of PHP array


Methods for judging php array dimensions (php array length)


php array, php array length _php tutorial

Related Article

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.