检查多维数组的所有值是否为空

时间:2018-06-22 15:05:07

标签: php laravel multidimensional-array isnull is-empty

我有一个像这样的多维数组,我不想检查所有“ open_at”和“ closed_at”值是否均为NULL。

array:7 [▼
0 => array:2 [▼
 0 => array:2 [▼
  "open_at" => null
   "closed_at" => "11:03"
 ]
 1 => array:2 [▼
  "open_at" => "13:00"
  "closed_at" => "16:00"
 ]
]
1 => array:2 [▼
 0 => array:2 [▼
  "open_at" => "09:00"
  "closed_at" => "12:00"
 ]
 1 => array:2 [▼
   "open_at" => "12:30"
   "closed_at" => "17:00"
 ]
]
2 => array:2 [▼
0 => array:2 [▼
  "open_at" => "08:00"
  "closed_at" => "18:00"
]
1 => array:2 [▼
  "open_at" => null
  "closed_at" => null
]
]
3 => array:2 [▼
 0 => array:2 [▼
   "open_at" => null
   "closed_at" => null
 ]
 1 => array:2 [▼
   "open_at" => null
   "closed_at" => null
 ]
]
...

我已经尝试过多个for和foreach循环,但都没有成功...

for ( $i = 0; $i <6 ; $i++) {
  for ($j = 0; $j < 2; $j++) {
        if(empty($hours[$i][$j]["open_at"])){
            $null="complete";
        }
        else{
            $null="empty";
        }
        return $null;
       }

    }

仅当所有“ open_at”和“ closed_at”值都设置为NULL时,才应将数组检查为空。 如上例所示,可以将第一个值设置为NULL,但在这种情况下,不应将数组检查为空。

目标是仅在所有“ open_at”和“ closed_at”都设置为NULL时才执行下面的代码。

$hours = $request->get('hours');  

//check if empty here

foreach ($hours as $key => $period) {
foreach($period as $attribute => $value){
    $shops_hour = new Shops_hour();
    $shops_hour->shop_id=$shop->id;
    $shops_hour->day=$key;
    $shops_hour->period=$attribute;
    $shops_hour->open_at=$hours[$key][$attribute]["open_at"];
    $shops_hour->closed_at=$hours[$key][$attribute]["closed_at"];
    $shops_hour->save();
        }
    }

先谢谢您

2 个答案:

答案 0 :(得分:1)

使用一个递归函数,如果其中包含的所有值都为null,则该函数将返回true:

function all_null_recursive($arr)
{
    foreach ($arr as $item) {

        /* if the item is an array
           and the function itself found something different from null */
        if (is_array($item) && all_null_recursive($item) === false) {
            return false;

        // if the item is not an array and different from null
        } elseif (!is_array($item) && $item !== null) {
            return false;
        }
    }

    // always found null, everything's good
    return true;
}

测试:
要测试的2个数组。

$foo = [
    0 => [
        0 => [
            "open_at" => null,
            "closed_at" => "11:03"
        ],
        1 => [
            "open_at" => "13:00",
            "closed_at" => "16:00"
        ],
    ],
    1 => [
        0 => [
            "open_at" => "09:00",
            "closed_at" => "12:00"
        ],
        1 => [
            "open_at" => "12:30",
            "closed_at" => "17:00"
        ],
    ],
    2 => [
        0 => [
            "open_at" => "08:00",
            "closed_at" => "18:00"
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    3 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ]
    ]
];

$foo_2 = [
    0 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    1 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    2 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ],
    ],
    3 => [
        0 => [
            "open_at" => null,
            "closed_at" => null
        ],
        1 => [
            "open_at" => null,
            "closed_at" => null
        ]
    ]
];

测试:

var_dump(all_null_recursive($foo));    // bool(false) 
var_dump(all_null_recursive($foo_2));  // bool(true)

答案 1 :(得分:0)

@jeff方法有效,谢谢

        $foundOneWithaValue = "0";
    foreach ($hours as $key => $period) {
       foreach($period as $attribute => $value){
            if(!empty($hours[$key][$attribute]["open_at"]) || (!empty($hours[$key][$attribute]["closed_at"])) ) {
                $foundOneWithaValue ++;
            }
        }
    }  

    if($foundOneWithaValue != 0)
    {
        foreach ($hours as $key => $period) {
           foreach($period as $attribute => $value){
                $shops_hour = new Shops_hour();
                $shops_hour->shop_id=$shop->id;
                $shops_hour->day=$key;
                $shops_hour->period=$attribute;
                $shops_hour->open_at=$hours[$key][$attribute]["open_at"];
                $shops_hour->closed_at=$hours[$key][$attribute]["closed_at"];
                $shops_hour->save();
            }
        }
    }