Home >Backend Development >PHP Problem >How to get how many elements there are in an array in php
php method to get how many elements there are in an array: 1. Use the count() function to count the number of all elements in the array, the syntax is "count(array,mode)"; 2. Use sizeof( ) function, which can calculate the number of elements in an array, the syntax is "sizeof(array,mode)".
The operating environment of this tutorial: windows7 system, PHP version 7.1, DELL G3 computer
I want to count the number of array elements in PHP In fact, it is very simple. PHP provides us with two functions, namely count() and sizeof() functions.
In fact, 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.
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)
The parameter description is as follows:
Example 1: Count the number of elements in a one-dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); //输出语句 var_dump($arr); echo "数组长度为:".count($arr); ?>
Example 2: Count the number of elements in a two-dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $arr= array( "张三", 25, array("高数","PHP教程","英语") ); //输出语句 var_dump($arr); echo "数组长度为:".sizeof($arr,1); ?>
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to get how many elements there are in an array in php. For more information, please follow other related articles on the PHP Chinese website!