We introduce the operations of arrays in PHP, such as deleting specified values in the array or determining whether there is a value in the array or clearing null values. Students in need can refer to it.
First, let’s take a look at the various methods on how PHP operates null values in arrays
implode(); For this function, please refer to http://www.bKjia.c0m/phper/29/2dc95be9381b4bb1753083c09fda1a36.htm
Use implode() to output the array as a string and determine whether the output string is empty. At first glance, it seems to be a good method, but unfortunately, like the previous point, it does not work for arrays with more than two dimensions. For example:
The code is as follows
代码如下 |
复制代码 |
$arr= array(array(),array(),array());
$str = implode(',',$arr);
if(empty($str)) echo "空";
else echo "非空";
|
|
Copy code
|
$arr= array(array(),array(),array());
代码如下 |
复制代码 |
$arr= array("","","");
echo count($arr);
|
$str = implode(',',$arr);
if(empty($str)) echo "empty";
代码如下 |
复制代码 |
$arr= array("d","s","");
echo in_array('', $arr); |
else echo "not empty";
代码如下 |
复制代码 |
$arr= array("","","");
if(empty($arr)) echo "空";
else echo "非空";
|
Obviously $arr is a two-dimensional array containing three empty arrays, which should be considered empty, but the output is indeed non-empty. Judgment failed.
3. count(); Please refer to http://www.bKjia.c0m/w3school/php/func_array_count.htm
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;
}
|
$arr= array("","",""); |
echo count($arr);
4. in_array('', $arr)); For function usage, please 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 show that there are empty elements in the array, but cannot prove that the array is empty. Obviously not.
5. empty(); For function usage, please refer to http://www.bKjia.c0m/so/php+empty()
This cpyeh feels similar to the previous methods
The code is as follows
|
Copy code
|
$arr= array("","","");
if(empty($arr)) echo "empty";
else echo "not empty";
The result is still non-empty
6. Use strlen(). If there is no content, the length seems to be 1
Combined with the above example, we write a complete deletion of empty elements 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;
}
http://www.bkjia.com/PHPjc/632230.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632230.htmlTechArticleWe introduce the operation of arrays 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, students in need can refer to it. First let’s take a look at...
|
|
|
|