How to calculate the length of an array in php? How to get the length of php array

黄舟
Release: 2023-03-17 14:24:02
Original
21816 people have browsed it

php array How to get the length, PHP provides us with two functions to calculate the length of one-dimensional array, I believe you are not very clear about the function of getting the length of the array, today We will take you to learn more about how to obtain the length of an array in PHP~

How to calculate the length of an array in php? How to get the length of php array

The method of obtaining the length of an array in PHP is very simple. PHP provides us with two functions to calculate an array length. The length of a dimensional array, such as count and sizeof, can directly count the length of the array. Let's look at a few examples below.

How to get the length of an array in php, use the php function count(), or sizeof()
For example:

The code is as follows:

$arr = Array('0','1','2','3','4'); 
echo count($arr);
// 输出 5
$arr = array('A','B','C');
echo sizeof($arr);
//输出3
Copy after login

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 Without a set variable, the number of array elements returned is 0;
The functions of the two functions are the same. According to the manual, sizeof() is an alias of the function count().

Then how to count the length of multidimensional array? Continue to look at the example
For example, the array you read is a two-dimensional array:

The code is as follows:

<?php
$arr=array(
                 0=>array(&#39;title&#39; => &#39;新闻1&#39;, &#39;viewnum&#39; => 123, &#39;content&#39; => &#39;ZAQXSWedcrfv&#39;),
                 1=>array(&#39;title&#39; => &#39;新闻2&#39;, &#39;viewnum&#39; => 99, &#39;content&#39; => &#39;QWERTYUIOPZXCVBNM&#39;)
                );
?>
Copy after login

If you want to count The length of the array $arr, which means that the two-dimensional array only has two news items, and the number you want is also 2. However, if you use count($arr) in different versions of PHP, the statistical results will be different;
Later, I found in 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 multi-dimensional arrays are not detected. (Array within an array);
1 (or COUNT_RECURSIVE) is for detecting multi-dimensional arrays,
So if you want to determine 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 {
  .....
}
?>
Copy after login

You can use code like this to test the function:

The code is as follows:

<?php
$arr=array(                 
0=>array(&#39;title&#39; => &#39;新闻1&#39;, &#39;viewnum&#39; => 123, &#39;content&#39; => &#39;ZAQXSWedcrfv&#39;),                 
1=>array(&#39;title&#39; => &#39;新闻2&#39;, &#39;viewnum&#39; => 99, &#39;content&#39; => &#39;QWERTYUIOPZXCVBNM&#39;)               
);
echo &#39;不统计多维数组:&#39;.count($arr,0);//count($arr,COUNT_NORMAL)
echo "<br/>";
echo &#39;统计多维数组:&#39;.count($arr,1);//count($arr,COUNT_RECURSIVE)
?>
Copy after login

Summary:

This article introduces two functions for obtaining the length of an array. I believe everyone has a better understanding of this and can choose the appropriate one according to your needs. I hope it will be useful to you. Work helps!

Related recommendations:

Method to determine php array dimensions (php array length)

Example of php array length function

php array, php array length_PHP tutorial

The above is the detailed content of How to calculate the length of an array in php? How to get the length of php array. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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