在2D数组中检查值是否为空的最简单方法是什么?

时间:2021-12-29 21:37:52

For example, I have an array like this:

例如,我有一个这样的数组:

array(4) ( "a" => string(0) "" "b" => string(0) "" "c" => string(0) "" "d" => string(0) "" )

array(4)(“a”=> string(0)“”“b”=> string(0)“”“c”=> string(0)“”“d”=> string(0)“”)

None of given values should be empty.

给定值都不应为空。

At the moment, I use this:

目前,我用这个:

if (!empty($_POST['x']['a']) && !empty($_POST['x']['b']) && !empty($_POST['x']['c']) && !empty($_POST['x']['d']))

...and that sucks from readability aspect.

......从可读性方面来说很糟糕。

Note: Array is associative.

注意:数组是关联的。

4 个解决方案

#1


6  

count(array_filter($_POST['x'])) === 4

Some Explanation: Empty() is the Opposite of a Boolean Variable, array_filter removes all elements that equal false (which is !empty()) and this count must match the expectation of 4 elements then.

一些解释:Empty()是布尔变量的相反,array_filter删除所有等于false的元素(即!empty()),此计数必须与4个元素的期望相匹配。

If the number of elements is defined by the total of elements submitted (empty or not), use count() instead the magic number:

如果元素的数量由提交的元素总数(空或非)定义,则使用count()代替幻数:

if (count(array_filter($_POST['x'])) === count($_POST['x']))
{
    echo 'No empty elements in $_POST["x"]!';
}

#2


0  

Did you check the array_reduce function ?

你检查了array_reduce函数吗?

function all_empty($v,$w){
   $v .= $w;
   return $v;
}
if(array_reduce($_POST['x'],'all_empty','')==''){

I haven't tested, but you can give this a try

我没有测试过,但你可以尝试一下

#3


0  

EDIT: (in response to comments)

编辑:(回应评论)

You can encapsulate the "uncool" logic in a function and call it with a one-liner:

您可以在函数中封装“uncool”逻辑并使用单行调用它:

if ( check_for_empty_values( $_POST ) ) { // Do something }

The encapsulated checking logic:

封装的检查逻辑:

function check_for_empty_values( $data ) {
    $valid = true;
    foreach ( $data as $element ) {
        if ( is_array( $element) ) {
            foreach ( $element as $subelement ) {
                if ( empty( $subelement ) ) {
                    $valid = false;
                }
            }
        }
    }

    return $valid;
}

#4


-2  

for($_POST as $key => $value) {
    if( !empty($value) ) {
      // Do stuff.    

    }
}

#1


6  

count(array_filter($_POST['x'])) === 4

Some Explanation: Empty() is the Opposite of a Boolean Variable, array_filter removes all elements that equal false (which is !empty()) and this count must match the expectation of 4 elements then.

一些解释:Empty()是布尔变量的相反,array_filter删除所有等于false的元素(即!empty()),此计数必须与4个元素的期望相匹配。

If the number of elements is defined by the total of elements submitted (empty or not), use count() instead the magic number:

如果元素的数量由提交的元素总数(空或非)定义,则使用count()代替幻数:

if (count(array_filter($_POST['x'])) === count($_POST['x']))
{
    echo 'No empty elements in $_POST["x"]!';
}

#2


0  

Did you check the array_reduce function ?

你检查了array_reduce函数吗?

function all_empty($v,$w){
   $v .= $w;
   return $v;
}
if(array_reduce($_POST['x'],'all_empty','')==''){

I haven't tested, but you can give this a try

我没有测试过,但你可以尝试一下

#3


0  

EDIT: (in response to comments)

编辑:(回应评论)

You can encapsulate the "uncool" logic in a function and call it with a one-liner:

您可以在函数中封装“uncool”逻辑并使用单行调用它:

if ( check_for_empty_values( $_POST ) ) { // Do something }

The encapsulated checking logic:

封装的检查逻辑:

function check_for_empty_values( $data ) {
    $valid = true;
    foreach ( $data as $element ) {
        if ( is_array( $element) ) {
            foreach ( $element as $subelement ) {
                if ( empty( $subelement ) ) {
                    $valid = false;
                }
            }
        }
    }

    return $valid;
}

#4


-2  

for($_POST as $key => $value) {
    if( !empty($value) ) {
      // Do stuff.    

    }
}