PHP cannot use the count function provided by php to obtain the length of any array to implement custom development. Let's take a look at how to detect the length of any array, the specific operations are as follows. PHP cannot use the count function provided by php to obtain the length of any array to implement custom development. Let's take a look at how to detect the length of any array, the specific operations are as follows.
Script ec (2); script
PHP often processes arrays with unknown depth. In this case, recursion can be used, but it is better to know the array depth.
Obtain the array length:
The Code is as follows: |
|
/** * @ Description get the array Length * @ Param array * @ Return int length of array */ Function array_len ($ array = array ()){ $ Len = 1; Foreach ($ array as & $ value ){ If (is_array ($ value )){ $ Len = array_len ($ value) + 1; } } Return $ len; } |
Test:
The Code is as follows: |
|
$ Arr = array ( Array ( Array ( Array ( Array ('id' => 1) ) ) ) ); $ Arr_len = array_len ($ arr ); Var_dump ($ arr_len ); // Result Int (5) |