从 PHP 中移除空数组元素
要在 PHP 中移除空数组元素,代码如下 −
样例
<?php
$my_array = array("This", 91, '', null, 102, "is", false, "a", "sample", null);
foreach($my_array as $key => $val)
if(empty($val))
unset($my_array[$key]);
echo "After removing null values from the array, the array has the below elements -";
foreach($my_array as $key => $val)
echo ($my_array[$key] ."<br>");
?>输出
After removing null values from the array, the array has the below elements -This 91 102 is a sample
定义了一个包含字符串、数字和‘null’值的数组。使用 ‘foreach’ 循环迭代这些元素,如果一个值为 null,则从数组中删除该元素。重新展示相关数组,其中不包含 null 值。
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP