Home > Backend Development > PHP Problem > How to determine the dimensions of an array in php

How to determine the dimensions of an array in php

藏色散人
Release: 2023-03-03 21:46:02
Original
2317 people have browsed it

php method to determine how many dimensions an array is: first create a PHP sample file; then define a foreachArray method; then use the "is_array" function to detect whether the variable is an array; finally determine whether the array is an array through loop traversal Just a few dimensions.

How to determine the dimensions of an array in php

Recommended: "PHP Video Tutorial"

PHP Determines the Dimension of an Array

/**
 * 判断数组为几维数组 可优化
 * @param array $array
 * @param int $count
 * @return int
 */
function foreachArray($array = [], $count = 1){
    if (!is_array($array)){
        return $count;
    }
    foreach ($array as $value){
        $count++;
        if (!is_array($value)){
            return $count;
        }
        return foreachArray($value, $count);
    }
}
Copy after login

Related introduction:

is_array() function is used to detect whether a variable is an array.

Syntax

bool is_array ( mixed $var )
Copy after login

Parameter description:

$var: Variable to be detected.

Return value

If the detected variable is an array, TRUE is returned, otherwise FALSE is returned.

The above is the detailed content of How to determine the dimensions of an array in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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