How to count the total number of elements in an array in php

青灯夜游
Release: 2023-03-17 18:38:02
Original
1429 people have browsed it

The count() or sizeof() function can be used in php to count the number of elements in the array; sizeof() is an alias of count(), so the function and usage of sizeof() are completely the same as count() same. The count() function can count the number of all elements in the array, or the number of attributes in the object. The syntax is "count(array,mode)" and the parameter "mode" is set to "COUNT_RECURSIVE" or "1" to calculate the number of multi-dimensional arrays. The number of elements.

How to count the total number of elements in an array in php

The operating environment of this tutorial: windows7 system, PHP8 version, DELL G3 computer

It is actually very difficult to count the number of array elements in PHP Simple, PHP provides us with two functions, namely count() and sizeof() functions.

Note: The sizeof() function is an alias of the count() function, that is, the function and usage of the sizeof() function are exactly the same as the count() function.

Example:

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
//输出语句
var_dump($arr);
echo "数组长度为:".count($arr);
echo "<br>数组长度为:".sizeof($arr);
?>
Copy after login

How to count the total number of elements in an array in php

The above example counts the number of elements of a one-dimensional array, but sometimes we need to count the elements of two-dimensional, three-dimensional and other multi-dimensional arrays How to calculate the number?

In fact, it is very simple. It is still the count() or sizeof() function. You only need to set the second parameter to it, and the value is COUNT_RECURSIVE or 1.

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array(
"张三",
25,
array("高数","PHP教程","英语")
);
//输出语句
var_dump($arr);
echo "数组长度为:".sizeof($arr,1);
?>
Copy after login

How to count the total number of elements in an array in php

After reading the above output, are you confused? There are not only 5 elements in the array ("张三", 25, "高书", "PHP Tutorial", "English"), why does the array length displayed in the result not be 5, but What about 6?

In fact, this is because at this time, the count() function loops to count all elements in the two-dimensional array, "array("高num","PHP Tutorial","English")" It will be counted once as a whole, and the elements inside it ("高书", "PHP Tutorial", "English") will be counted again. , so the final result is 6.

Extended knowledge: PHP count() /sizeof() function

Let’s introduce the count() function to you to understand these two functions.

The count() function can count the number of all elements in the array, or the number of attributes in the object. Its syntax format is as follows:

count(array,mode)
Copy after login

The parameter description is as follows:

  • array: is the array or object to be counted;

  • mode: is an optional parameter and can be omitted.

    If the mode parameter is omitted, or set to COUNT_NORMAL or 0, the count() function will not detect multidimensional arrays;

    If mode Set to COUNT_RECURSIVE or 1, the count() function will recursively count the number of elements in the array, which is especially useful for calculating the number of elements in multi-dimensional arrays.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to count the total number of elements in an array in php. 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