Home  >  Article  >  Backend Development  >  How to calculate the length of an array in php? How to get the length of php array

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

黄舟
黄舟Original
2017-11-15 09:57:5621649browse

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

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:

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, 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:

0 )
{
  .....
} else {
  .....
}
?>

You can use code like this to test the function:

The code is as follows:

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

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!

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