Php deletes null values or specified values in an array.

Source: Internet
Author: User

We will introduce the operations on arrays in php, delete the specified values in the array, or determine whether there are values in the array or clear null values. If you need such operations, you can refer to them.

First, let's take a look at several methods for php to operate on null values in arrays.

Implode (); this function can refer to http://www.bKjia. c0m/phper/29/2 dc95be9381b4bb1753083c09fda1a36.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.
Iii. count (); see http://www.bKjia. c0m/w3school/php/func_array_count.htm

The Code is as follows: Copy code

$ Arr = array ("","","");
Echo count ($ arr );

Iv. in_array ('', $ arr); function usage can refer to http://www.bKjia. c0m/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.bKjia. c0m/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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.