Home >Backend Development >PHP Problem >How to query how many key value pairs there are in an array in php

How to query how many key value pairs there are in an array in php

青灯夜游
青灯夜游Original
2022-02-17 13:07:542832browse

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)".

How to query how many key value pairs there are in an array in php

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:

  • $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 is set to COUNT_RECURSIVE or 1, the count() function Will recursively count the number of elements in the array, especially useful for counting the number of elements in multi-dimensional arrays.

Example:

One-dimensional array:

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array("香蕉","苹果","梨子","橙子","橘子","榴莲");
//输出语句
var_dump($arr);
echo "数组有:".count($arr)." 个键值对";
?>

How to query how many key value pairs there are in an array in php

Two-dimensional array:

<?php
header("Content-type:text/html;charset=utf-8");
$arr= array
("张三",
25,
    array("高数","PHP教程","英语"),
);
//输出语句
var_dump($arr);
echo "数组有:".sizeof($arr,1)." 个键值对";
?>

How to query how many key value pairs there are in an array in php

Three-dimensional array

<?php
header("Content-type:text/html;charset=utf-8");
$arr = array(
        &#39;安徽&#39; => array(
            &#39;合肥&#39;=>array(&#39;蜀山区&#39;,&#39;长丰县&#39;,&#39;肥东&#39;),
			&#39;宿州&#39;=>array(&#39;墉桥区&#39;,&#39;灵璧县&#39;,&#39;泗县&#39;)
        ),
        &#39;河南&#39; => array(
            &#39;洛阳&#39;=>array(&#39;西工区&#39;,&#39;老城区&#39;,&#39;孟津县&#39;),
            &#39;郑州市&#39;=>array(&#39;中原区&#39;,&#39;金水区&#39;)
        )
);
//输出语句
var_dump($arr);
echo "数组有:".sizeof($arr,1)." 个键值对";
?>

How to query how many key value pairs there are in an array in php

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!

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