Count in PHP gets the length of the multidimensional array and determines whether the multidimensional array is empty instance code

Source: Internet
Author: User
This article mainly introduces the method of retrieving the length of multi-dimensional array in PHP, the example analyzes the principle of the array and summarizes the method of calculating the length of the array, which is very useful, and the friends who need can refer to

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:

The code is as follows:

$numb =array (             Array (10,15,30), Array (10,15,30), Array (10,15,30));   echo Count ($numb, 1);

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:

The code is 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:

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.


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:

The code is as follows:

<?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:

The code is as follows:

<?phpif (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 ' = ' 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)?>

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

This is the instance code that determines whether a multidimensional array is empty, and the code is as follows:

<?php//determines whether an array is empty/**array (); An empty Array (array (), array (), array ()); An empty Array (array (), array (array (), array (1=>1)), array ()); Non-empty */function is_array_null ($value) {if (empty ($value)) {return $value;} Else{return Is_array ($value)? Array_map (' Array_null ', $value): Addslashes ($value);}}
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: 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.