Sometimes our array is dynamically generated, we do not know the array is a few dimensions, the following I would like to introduce to you the PHP judgment array is a one-dimensional, two-dimensional, or multidimensional method, there is a need to understand the friend can enter the reference.
Lei
The code is as follows |
Copy Code |
/** * Returns the dimension of the array * @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); } ?> |
Example Two
You can determine whether it is one-dimensional, two-dimensional, or several-dimensional arrays:
The code is as follows |
Copy Code |
function Getmaxdim ($vDim) { if (!is_array ($vDim)) return 0; Else { $max 1 = 0; foreach ($vDim as $item 1) { $t 1 = $this->getmaxdim ($item 1); if ($t 1 > $max 1) $max 1 = $t 1; } Return $max 1 + 1; } } Verified can be used. Test $arr =array (' Yiyi ' =>1212, ' haha ' =>array (' Heihei ' =>array (Array ("a")), "B")); echo Getmaxdim ($arr); Results 4 |
http://www.bkjia.com/PHPjc/631274.html www.bkjia.com true http://www.bkjia.com/PHPjc/631274.html techarticle sometimes our array is dynamically generated, we do not know the array is a few dimensions, the following I would like to introduce to your classmates PHP judgment array is one-dimensional, two-dimensional, or multidimensional methods, there is a need ...