Home > Article > Backend Development > In php, count obtains the length of a multi-dimensional array and determines whether the multi-dimensional array is empty. Example code
This article mainly introduces the method of counting in php to obtain the multidimensional array length. The example analyzes the principle of the array and summarizes the method of calculating the array length. It is of great practical value. Friends in need can refer to it. Next
The example of this article describes the implementation method of counting to obtain the length of a multi-dimensional array in PHP. Share it with everyone for your reference. The specific analysis is as follows:
First, let’s take a look at the running results of the following program:
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 that if mode is set to COUNT_RECURSIVE (or 1) in the B
count function, the number of elements in the array in the multi-dimensional array will be calculated recursively. number (i.e. your result is 12). If mode is not set, it defaults to 0. Multidimensional arrays (arrays within arrays) are not detected (result 3).
The first thing to traverse is the outer arrayIt turns out that there are two elements ("color1", "color2", "color3") and then traverse ("color1" for 3
, "color2", "color3") The array results in 9 elements as 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 the array is defined in other ways, 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 way, 3 will be output, because the array in php It is not required that the index must be continuous. 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 has been optimized in many ways, so you can use it as a real array, or list (vector), hash table (an implementation of a graph), dictionary, set, stack, queueand many more possibilities. Since you can use another PHP array as a value, you can also simulate a tree easily.
Example:
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' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'), 1=>array('title' => '新闻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 news, what you want The number is also 2, but if you use count($arr) in different versions of PHP, the statistical results are different;
I later found out in the php manual that the count function has a second parameters, explained as follows: The
count function has two parameters:
0 (or COUNT_NORMAL) is the default, and multi-dimensional arrays (arrays within arrays) are not detected;
1 (or COUNT_RECURSIVE) is to detect multi-dimensional arrays,
So if you want to judge whether the read array $arr has news information, you need to write it like this:
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' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'), 1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM') ); echo '不统计多维数组:'.count($arr,0);//count($arr,COUNT_NORMAL) echo "<br/>"; echo '统计多维数组:'.count($arr,1);//count($arr,COUNT_RECURSIVE) ?>
Okay, at this point, you have solved the problem of getting the second function in php The length of the first dimension of a dimensional or multidimensional array.
The following is an example code to determine whether a multi-dimensional array is empty. The code is as follows:
<?php //判断一个数组是否为空 /** array( ); 空 array( array( ), array( ), array( ) ); 空 array( array( ), array( array( ), array( 1=>1 ) ), array( ) ); 非 空 */ function is_array_null($value) { if (empty($value)) { return $value; } else { return is_array($value) ? array_map('array_null', $value) : addslashes($value); } }
The above is the detailed content of In php, count obtains the length of a multi-dimensional array and determines whether the multi-dimensional array is empty. Example code. For more information, please follow other related articles on the PHP Chinese website!