我有一个阵列,我只是将其打印为 -
的print_r($数据);
它显示输出as-
Array
(
[0] => Array
(
[0] => Title
[1] => Featured Image
[2] => Catagories
[3] => Tags
[4] => Content
)
[1] => Array
(
[0] => title 1
[1] => img1.jpg
[2] => cat 1
[3] => tag 1
[4] => post 1 content
)
[2] => Array
(
[0] => title 2
[1] => img2.jpg
[2] => cat2
[3] => tag 2
[4] => post 2 content
)
[3] => Array
(
[0] => title 3
[1] => img3.jpg
[2] => cat3
[3] => tag3
[4] => post 3 content
)
[4] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[5] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
[6] => Array
(
[0] =>
[1] =>
[2] =>
[3] =>
[4] =>
)
)
我只想从数组中删除空值或空值
我尝试了array_dif()和array_filter(),但我仍然无法删除空值。
这怎么可能?
感谢。
答案 0 :(得分:1)
您可以遍历数组,查找空变量并使用unset删除它们。
此代码将遍历并检查每个数组中第一个值的长度是否至少为一个字符长,如果不是则取消设置。
<?php
foreach($data as $key => $value) {
if(!isset($value[0][0]))
unset($data[$key]);
}
此代码将以类似的方式循环遍历数组,除了检查每个数组的每个值以确定是否应保留其parrent数组或将其保留为未设置。
<?php
foreach($data as $key => $values) {
foreach($values as $value) {
if(isset($value[0]))
continue 2;
}
unset($data[$key]);
}
答案 1 :(得分:1)
你应该使用array_filter和一个也对子数组运行array_filters的函数,如果过滤后的子数组变空,则返回false。
<?php
$array = Array(
Array(1, 2, 3),
Array(null, null, null),
Array(false, false, false),
Array(3, 2, 1)
);
$filtered = array_filter($array, function($elem) {
return count(array_filter($elem));
});
print_r($filtered);
?>