Method for obtaining length of multidimensional array by count in php

  • 2021-07-24 10:27:01
  • OfStack

In this paper, an example is given to describe the implementation method of obtaining the length of multidimensional array by count in php. Share it for your reference. The specific analysis is as follows:

Let's take a look at the following program running 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 in the multidimensional array is recursively calculated (that is, 12 of your result). 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 array array, which shows that two elements ("color1", "color2", "color3") are 3
Traversing the array again ("color1", "color2", "color3") yields 9 elements for 9
The result is 3+9=12

Reference example:

<?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, such as:

<?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 continuous. The reference manual has the following 1 paragraph:

Array:

The array in PHP is actually an ordered graph. Figure is a type of mapping values to keys. This type is optimized in many ways, so you can use it as a real array, or lists (vectors), hash tables (one implementation of graphs), dictionaries, collections, stacks, queues, and more. Because you can use another PHP array as the value, you can also easily simulate the tree.

Example:

Get the length of the first dimension of a 2-or multi-dimensional array, which is a common program judgment. For example, the array you read is a 2-dimensional array:

<?php
$arr=array(
 0=>array('title' => ' News 1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
 1=>array('title' => ' News 2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
?>

If you want to count the length of the array $arr, that is to say, there are only two news items in the 2-dimensional array, and the number you want is also 2, but if you use different versions of php ($arr), the statistical results are different;

Later, it was found in the php manual that the count function also has a second parameter, which is explained as follows:

The count function takes two arguments:

0 (or COUNT_NORMAL) is the default and does not detect multidimensional arrays (arrays within arrays);
1 (or COUNT_RECURSIVE) is used to detect multidimensional arrays,

So if you want to judge whether the read array $arr has news information, you should write this:

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

You can test this function with code like this:

<?php
$arr=array(
 0=>array('title' => ' News 1', 'viewnum' => 123, 'content' => 'ZAQXSWedcrfv'),
 1=>array('title' => ' News 2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
);
echo ' Do not count multidimensional arrays: '.count($arr,0);//count($arr,COUNT_NORMAL)
echo "<br/>";
echo ' Statistical multidimensional array: '.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>

Well, at this point, the problem of getting the first dimension length of a 2-or multi-dimensional array in php has been solved.

I hope this article is helpful to everyone's PHP programming.


Related articles: