search
HomeBackend DevelopmentPHP ProblemHow to query the length of a multidimensional array in php

How to query the length of a multidimensional array in php

May 27, 2021 pm 06:54 PM
phpMultidimensional Arraysarray length

In PHP, you can use the count() function to query the length of a multi-dimensional array. This function can return the number of elements in the array. The syntax is "count(array,mode)"; when the value of the mode parameter is 1 When counting multidimensional arrays, count the number of all elements in the multidimensional array recursively.

How to query the length of a multidimensional array in php

The operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

PHP obtains two-dimensional (multi-dimensional) Arrays of various lengths

<?php
$array_test = array(
	&#39;0&#39; => array(&#39;0&#39;=>"aa"),
	&#39;1&#39; => array(&#39;1&#39;=>"bb"),
);
$n1=count($array_test,0);//不计较多维数组,只取最外层数组下一层的长度,结果为2
$n2=count($array_test,1);//计较多维数组,只取最外层数组下下一层的长度,结果为2+2=4
//下面依此类推

echo ($n1."<br>");
echo ($n2);
?>

Output:

2
4

Description:

count() function returns the array The number of elements.

For objects, if you install the SPL extension, you can call the count function by implementing the Countable interface. The Countable interface has one and only one method, Countable::count(), which returns the return value of the count() function.

The syntax is as follows:

count(array,mode);
Parameters Description
array Required. Specifies the array to count.
mode Optional. Specifies the mode of the function. Possible values:
  • 0 - Default. Not counting all elements in a multidimensional array.
  • 1 - Recursively count the number of elements in an array (count all elements in a multidimensional array).

If the parameter mode is set to COUNT_RECURSIVE (or 1), count() will recursively calculate the array. Especially useful when calculating multi-dimensional arrays.

If the first parameter is not an array or an object that implements the Countable interface, the count function will return 1.

Note: The count function can detect recursion to avoid infinite loops, but will return an E_WARNING prompt when encountering infinite recursion or getting a value larger than expected.

Recommended learning: "PHP Video Tutorial"

The above is the detailed content of How to query the length of a multidimensional 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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.