如果任何元素为空,则删除数组 [英] Remove array if any element is empty
问题描述
我的数组具有以下结构。它是通过表单提交的文件数组。我有多个表单输入字段。但是,如果用户将某些字段留空,则该数组将显示如下。
然后将其写入数据库。我不想写空字段。
My array is of following structure. It is a array of files submitted through form. I have multiple input fields for form. However if the user leave some field blank, the array will be displayed like below. I am then writing it to my database. I dont want the empty fields to be written.
我尝试使用 array_filter()
函数,但不会工作,因为我的数组不是完全为空。错误元素设置为 4
。我该怎么做?
I tried using array_filter()
function, but it will not work since my array is not completely empty. The error element is set to 4
. How do i do it?
Array
(
[0] => Array
(
[name] => stock-photo-cup-icons-tea-and-coffee-raster-version-109119257.jpg
[type] => image/jpeg
[tmp_name] => /tmp/phpqWWM9X
[error] => 0
[size] => 30609
)
[1] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[2] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
[3] => Array
(
[name] =>
[type] =>
[tmp_name] =>
[error] => 4
[size] => 0
)
)
推荐答案
您可以将(可选)回调传递给 array_filter()
:
You can pass an (optional) callback to array_filter()
:
$filteredArray = array_filter($source, function($item) {
return ($item['size'] > 0 && $item['error'] === 0);
});
var_dump($filteredArray);
演示: http://codepad.viper-7.com/uocPT6
这篇关于如果任何元素为空,则删除数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!