Home > Backend Development > PHP Tutorial > Example code for how to determine the dimensions of an array and whether the array is empty in PHP

Example code for how to determine the dimensions of an array and whether the array is empty in PHP

怪我咯
Release: 2023-03-13 11:36:01
Original
1494 people have browsed it

The following is a detailed analysis and introduction to the code for determining the dimensions of the php array. For reference, the code is as follows:

<?php 
/** 
 * 返回数组的维度 
 * @param  [type] $arr [description] 
 * @return [type]      [description] 
 */
function arrayLevel($arr){ 
    $al = array(0); 
    function aL($arr,&$al,$level=0){ 
        if(is_array($arr)){ 
            $level++; 
            $al[] = $level; 
            foreach($arr as $v){ 
                aL($v,$al,$level); 
            } 
        } 
    } 
    aL($arr,$al); 
    return max($al); 
} 

$arr = array( 
    &#39;0&#39;=>&#39;0&#39;, 
); 

echo arrayLevel($arr); 
?>
Copy after login

The following is the method to determine if the array is empty: empty($arr), the code is as follows

$arr= array(""); 
$result = empty($arr); 
//$result = false 
$arr = array(); 
$result = empty($arr); 
//$result = true
Copy after login

The above is the detailed content of Example code for how to determine the dimensions of an array and whether the array is empty in PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template