Home >Backend Development >PHP Problem >How to query how many key value pairs there are in an array in php
In PHP, you can use the count() or sizeof() function to query how many key-value pairs there are in the array. Both functions can get the number of key-value pairs (elements) in the array; syntax " count($array, 1)" or "sizeof($array, 1)".
The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer
How to query an array in php How many key value pairs are there
To query how many key value pairs there are in the array is to query how many elements the array has, that is, the length of the array.
In PHP, two functions count() and sizeof() are provided to obtain the array length (one or more dimensions are acceptable, just set the second parameter to 1).
In fact, the sizeof() function is an alias of the count() function, so the usage of the two functions is consistent.
count($array , $mode) sizeof($array, $mode)
The parameter description is as follows:
Example:
One-dimensional array:
<?php header("Content-type:text/html;charset=utf-8"); $arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲"); //输出语句 var_dump($arr); echo "数组有:".count($arr)." 个键值对"; ?>
Two-dimensional array:
<?php header("Content-type:text/html;charset=utf-8"); $arr= array ("张三", 25, array("高数","PHP教程","英语"), ); //输出语句 var_dump($arr); echo "数组有:".sizeof($arr,1)." 个键值对"; ?>
Three-dimensional array
<?php header("Content-type:text/html;charset=utf-8"); $arr = array( '安徽' => array( '合肥'=>array('蜀山区','长丰县','肥东'), '宿州'=>array('墉桥区','灵璧县','泗县') ), '河南' => array( '洛阳'=>array('西工区','老城区','孟津县'), '郑州市'=>array('中原区','金水区') ) ); //输出语句 var_dump($arr); echo "数组有:".sizeof($arr,1)." 个键值对"; ?>
Recommended learning: "PHP Video Tutorial", " PHP ARRAY》
The above is the detailed content of How to query how many key value pairs there are in an array in php. For more information, please follow other related articles on the PHP Chinese website!