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

How to determine the dimensions of an array in php

藏色散人
藏色散人Original
2020-08-06 10:57:332270browse

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);
    }
}

Related introduction:

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

Syntax

bool is_array ( mixed $var )

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!

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