Home >Backend Development >PHP Tutorial >How to get the length of an array in php (with examples)_PHP tutorial

How to get the length of an array in php (with examples)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:25:52875browse

It is very simple to get the length of an array in PHP. PHP provides us with two functions to calculate the length of a one-dimensional array, such as count and sizeof, which can directly count the length of the array. Let's take a look at a few examples.
How to get the length of an array in php, use the php function count(), or sizeof()
For example:

Copy the code The code is as follows:

$arr = Array('0','1','2','3','4');
echo count($arr);
// Output 5
$arr = array('A','B','C');
echo sizeof($arr);
//Output 3

sizeof () and count() have the same purpose. Both functions can return the number of array elements. You can get the number of elements in a regular scalar variable. If the array passed to this function is an empty array, or a If there is no set variable, the number of array elements returned is 0;
The two functions have the same function. According to the manual, sizeof() is an alias of the function count().

So how to count the length of multi-dimensional arrays? Continue to look at the example
For example, the array you read is a two-dimensional array:
Copy the code The code is as follows:

$arr=array(
                        0=>array('title' => 'News 1', '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 has only two news, the number you want is also 2, but if you use count($arr) Different versions of PHP have different statistical results;
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 and does not detect multi-dimensional arrays (arrays within arrays);
1 (or COUNT_RECURSIVE) is for detecting multi-dimensional arrays,
so if you want to determine whether the read array $arr contains news information, just To write like this:
Copy code The code is as follows:

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

You can use code like this to test the function:
Copy the code The code is as follows:

$arr=array(
                                                                                                                                                                                                          array ),
1=>array('title' => 'News2', 'viewnum' => 99, 'content' => 'QWERTYUIOPZXCVBNM')
echo 'Do not count multi-dimensional arrays:'.count($arr,0);//count($arr,COUNT_NORMAL)

echo "
";
echo 'Count multi-dimensional arrays :'.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>

http://www.bkjia.com/PHPjc/824919.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/824919.htmlTechArticleIt is very simple to get the length of an array in PHP. PHP provides us with two functions to calculate the length of a one-dimensional array. , such as count and sizeof, can directly count the array length. Let’s take a look...
Statement:
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