如何检查php中所有空值的关联数组?

时间:2023-01-12 07:55:26

This is probably a very trivial question, but please bear with me.

这可能是一个非常微不足道的问题,但请耐心等待。

I am trying to read a lot of data into an array of associative arrays. The data contains a lot of empty arrays and arrays with keys set and but all null values. I want to ignore those and only push arrays with at least one key mapped to a non-null value. (The data comes from an excel sheet and it has lots of empty cells that are registered as "set" anyway.) So far I have tried:

我试图将大量数据读入关联数组数组。数据包含许多空数组和带有键集的数组,但所有空值都是如此。我想忽略那些,只推送至少有一个键映射到非空值的数组。 (数据来自excel表格,它有很多空单元格,无论如何都注册为“set”。)到目前为止,我尝试过:

if(!empty(${$small_dummy}))
array_push(${$big_dummy}, ${$small_dummy});

if(!empty($ {$ small_dummy}))array_push($ {$ big_dummy},$ {$ small_dummy});

That gets rid of the empty arrays but not the ones where all keys map to null. Is there a better way to do this than looping through the entire array and popping all null values?

这摆脱了空数组,但不是所有键映射到null的数组。有没有比循环遍历整个数组并弹出所有空值更好的方法呢?

3 个解决方案

#1


4  

Judging by the code you have already, you can change:

根据您已经拥有的代码判断,您可以更改:

if(!empty(${$small_dummy}))

to:

if(!empty(array_filter(${$small_dummy})))

That will filter out all empty values (values evaluating to FALSE to be precise) and check if the resulting array is empty. Also see the manual on array_filter().

这将过滤掉所有空值(评估为FALSE的值是准确的)并检查结果数组是否为空。另请参阅array_filter()上的手册。

Note that this would also filter 0 values so you might need to write a custom callback function for array_filter().

请注意,这也会过滤0值,因此您可能需要为array_filter()编写自定义回调函数。

#2


0  

You can try if(!array_filter($array)) { also

您可以尝试if(!array_filter($ array)){also

#3


0  

This isn't an ideal approach, but array_sum will return 0 if all values can't be cast to a numeric value. So :

这不是一种理想的方法,但如果所有值都不能转换为数值,array_sum将返回0。所以:

$small_dummy = array("a" => null, "foo", "", 0); 

if(array_sum($small_dummy) === 0) 

would pass. But this is only the way to go if you are expecting the values to be numeric.

会过去的。但是,如果您期望值为数字,那么这只是一种方法。

Actually, if the problem is that the array keys have values and therefor are not passing as empty(), the go with array_values:

实际上,如果问题是数组键有值而且因此没有传递为empty(),则使用array_values:

if(!empty(array_values(${$small_dummy})))

#1


4  

Judging by the code you have already, you can change:

根据您已经拥有的代码判断,您可以更改:

if(!empty(${$small_dummy}))

to:

if(!empty(array_filter(${$small_dummy})))

That will filter out all empty values (values evaluating to FALSE to be precise) and check if the resulting array is empty. Also see the manual on array_filter().

这将过滤掉所有空值(评估为FALSE的值是准确的)并检查结果数组是否为空。另请参阅array_filter()上的手册。

Note that this would also filter 0 values so you might need to write a custom callback function for array_filter().

请注意,这也会过滤0值,因此您可能需要为array_filter()编写自定义回调函数。

#2


0  

You can try if(!array_filter($array)) { also

您可以尝试if(!array_filter($ array)){also

#3


0  

This isn't an ideal approach, but array_sum will return 0 if all values can't be cast to a numeric value. So :

这不是一种理想的方法,但如果所有值都不能转换为数值,array_sum将返回0。所以:

$small_dummy = array("a" => null, "foo", "", 0); 

if(array_sum($small_dummy) === 0) 

would pass. But this is only the way to go if you are expecting the values to be numeric.

会过去的。但是,如果您期望值为数字,那么这只是一种方法。

Actually, if the problem is that the array keys have values and therefor are not passing as empty(), the go with array_values:

实际上,如果问题是数组键有值而且因此没有传递为empty(),则使用array_values:

if(!empty(array_values(${$small_dummy})))