Sometimes our array is dynamically generated, and we do not know how many dimensions the array is. next I will introduce you to php to determine whether the array is a one-dimensional, two-dimensional, or multi-dimensional method, friends who need to know
Sometimes our array is dynamically generated, and we do not know how many dimensions the array is. next I will introduce you to php to determine whether the array is a one-dimensional, two-dimensional, or multi-dimensional method, for more information, see.
Sample code:
-
- /**
- * Returns the array dimension.
- * @ 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 2: You can determine whether it is a one-dimensional, two-dimensional, or several-dimensional array. the code is as follows:
- Function getmaxdim ($ vDim)
- {
- If (! Is_array ($ vDim) return 0;
- Else
- {
- $ Max1 = 0;
- Foreach ($ vDim as $ item1)
- {
- $ T1 = $ this-> getmaxdim ($ item1 );
- If ($ t1> $ max1) $ max1 = $ t1;
- }
- Return $ max1 + 1;
- }
- }
- It has been verified and can be used.
- // Test
- $ Arr = array ('yiyi' => 1212, 'hahaha' => array ('heiheihei' => array ("a"), "B "));
- Echo getmaxdim ($ arr );
- // Result
- 4