Home > Backend Development > PHP Tutorial > How to implement count multidimensional array length statistics in PHP_PHP tutorial

How to implement count multidimensional array length statistics in PHP_PHP tutorial

WBOY
Release: 2016-07-13 16:56:17
Original
858 people have browsed it

How to implement count multi-dimensional array length statistics in PHP. Friends in need can refer to it.

The result of running the following program is ()

The code is as follows Copy code
 代码如下 复制代码

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

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

Answer: B

If mode is set to COUNT_RECURSIVE (or 1) in the count function, the number of elements in the array in the multi-dimensional array will be recursively calculated (that is, 12 in your result). If mode is not set, it defaults to 0. Multidimensional arrays (arrays within arrays) are not detected (result 3).
 代码如下 复制代码

$fruits = array (
array (1, 2,null,null, 5, 6),
array (1, 2,null,null, 5, 6),
);

echo(count($fruits[0]));
?>

First, the outer array array is traversed and there are two elements ("color1", "color2", "color3"), which is 3
 代码如下 复制代码

$fruits[0][0]=1;
$fruits[0][3]=1;
$fruits[0][4]=1;

echo(count($fruits[0]));
?>

Then traverse the ("color1", "color2", "color3") array and get 9 elements, which is 9

The result is 3+9=12

Reference

The code is as follows Copy code

$fruits = array (

Array (1, 2,null,null, 5, 6),
Array (1, 2,null,null, 5, 6),

);
代码如下 复制代码
$arr=array(
0=>array('title' => '新闻1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
                 1=>array('title' => '新闻2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
                );
?>
echo(count($fruits[0])); ?> You may be talking about arrays defined in other ways, such as directly using:
The code is as follows Copy code
$fruits[0][0]=1;<🎜> $fruits[0][3]=1;<🎜> $fruits[0][4]=1;<🎜> <🎜>echo(count($fruits[0]));<🎜> ?>
In this case, 3 will be output, because arrays in PHP do not require consecutive indexes. The reference manual has the following paragraph: Array An 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 a list (vector), a hash table (an implementation of a graph), a dictionary, a set, a stack, a queue and many more possibilities . It's also easy to simulate a tree since you can use another PHP array as a value. 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 Copy code
$arr=array(<🎜>                    0=>array('title' => 'News1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),                    1=>array('title' => 'News2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM') ); ?>

If you want to count the length of the array $arr, that is to say, the two-dimensional array only has two news, the number you want is also 2, but if you use count($arr) different versions of PHP, the statistical result is 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, multi-dimensional arrays (arrays within arrays) are not detected;
1 (or COUNT_RECURSIVE) is to detect multi-dimensional arrays,
So if you want to determine whether the read array $arr contains news information, you need to write it like this:

The code is as follows
 代码如下 复制代码
if(is_array($arr) && count($arr,COUNT_NORMAL)>0 )
{
  .....
} else {
  .....
}
?>
Copy code

代码如下 复制代码
$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 "
";
echo '统计多维数组:'.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>
if(is_array($arr) && count($arr,COUNT_NORMAL)>0 )

{

..... } else { ..... } ?>

You can test the function using code like this:
The code is as follows Copy code
$arr=array(                    0=>array('title' => 'News1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),                    1=>array('title' => 'News2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM') ); echo 'Do not count multi-dimensional arrays:'.count($arr,0);//count($arr,COUNT_NORMAL) echo "
"; echo 'Statistical multi-dimensional array:'.count($arr,1);//count($arr,COUNT_RECURSIVE) ?>
Okay, at this point, the problem of obtaining the first dimension length of a two-dimensional or multi-dimensional array in PHP has been solved http://www.bkjia.com/PHPjc/631599.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631599.htmlTechArticleFriends in need of the count multi-dimensional array length statistics implementation method in php can refer to it. The result of running the program below is (). The code is as follows. Copy the code ?$numb=array( array(10,15,30),ar...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template