PHP Delete null value in array or specify value operation _php Tutorial

Source: Internet
Author: User
We introduce the operation of the array in PHP, delete the specified value in the array or determine whether there is a value in the array or clear the empty value operation, the need for students can refer to.

Let's take a look at a number of ways that PHP can manipulate null values in an array

Implode (); This function can refer to the http://www.bKjia.c0m/phper/29/2dc95be9381b4bb1753083c09fda1a36.htm
Use implode () to output the array as a string to determine if the output is empty. It seems like a good way to start at first, but as with a little bit more than two-dimensional arrays. As an example:

The code is as follows Copy Code

$arr = Array (array (), array (), array ());
$str = Implode (', ', $arr);

if (empty ($STR)) echo "null";
else echo "non-null";

It is obvious that $arr is a two-dimensional array with three empty arrays, which should also be empty, but the output is indeed non-empty. Judgment failed.
III, COUNT (); Refer to 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 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 that there are empty elements in the array and cannot prove that the array is empty. It's not obvious.
Five, empty (); Function usage refer to http://www.bKjia.c0m/so/php+empty ()
This Cpyeh thinks it's the same as the previous methods.

The code is as follows Copy Code

$arr = Array ("", "", "");
if (empty ($arr)) echo "null";
else echo "non-null";

The result is still non-empty
Six, with strlen (), no content words as if the length of 1

In conjunction with the above example, we write a complete element that deletes an array of null values

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;
}

http://www.bkjia.com/PHPjc/632230.html www.bkjia.com true http://www.bkjia.com/PHPjc/632230.html techarticle we introduce the operation of the array in PHP, delete the specified value in the array or determine whether there is a value in the array or clear the empty value operation, the need for students can refer to. First, let's take a look at the Guan ...

  • 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.