First, let's take a look at several methods for php to operate on null values in arrays.
Implode (); this function can refer to the http://www.111cn.net/phper/29/2dc95be9381b4bb1753083c09fda1a36.htm
Use implode () to output the array as a string and determine whether the output string is null. It seems to be a good method at the beginning, but unfortunately it keeps up with 1.1. It won't work for arrays above two dimensions. For example:
| The code is as follows: |
Copy code |
|
$ Arr = array (), array (), array ());
$ Str = implode (',', $ arr );
If (empty ($ str) echo "null ";
Else echo "not empty "; |
Obviously, $ arr is a two-dimensional array containing three empty arrays. It should be empty, but the output is indeed non-empty. Failed.
3, count (); can refer to the http://www.111cn.net/w3school/php/func_array_count.htm
| The code is as follows: |
Copy code |
|
$ Arr = array ("","","");
Echo count ($ arr ); |
IV. in_array ('', $ arr); for function usage, see http://www.111cn.net/phper/24/c5b81a8af14b1c0928eea343f59b454a.htm
| The code is as follows: |
Copy code |
$ Arr = array ("d", "s ","");
Echo in_array ('', $ arr ); |
This can only indicate empty elements in the array, and it cannot be proved that the array is empty. Obviously not.
V. empty (); function usage can refer to http://www.111cn.net/so/php+empty ()
This cpyeh is similar to the previous methods.
| The code is as follows: |
Copy code |
|
$ Arr = array ("","","");
If (empty ($ arr) echo "null ";
Else echo "not empty "; |
The result is not empty.
6. Use strlen (). If there is no content, it seems that the length is 1.
In combination with the above instance, we will write a complete element to delete null values in the array.
| The code is as follows: |
Copy code |
|
Function array_remove_key ($ array, $ keys)
{
$ Num = count ($ keys );
$ Num_last = $ num-1;
$ This_array_0 = & $ array;
$ Last_key = $ keys [$ num_last];
For ($ I = 0; $ I <$ num_last; $ I ++)
{
$ This_key = $ keys [$ I];
$ This_var_name = 'This _ array _ '. $ I;
$ Next_var_name = 'This _ array _ '. ($ I + 1 );
If (! Array_key_exists ($ this_key, $ this_var_name )){
Break;
}
$ Next_var_name = & $ {$ this_var_name} [$ this_key];
}
Unset ($ {$ next_var_name} [$ last_key]);
Return $ array;
} |