php - 检查多维数组的所有值是否为空
问题描述
我有一个这样的多维数组,我不想检查所有“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();
}
}
先感谢您,
解决方案
如果包含的所有值都为空,则使用将返回 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)
推荐阅读
- google-cloud-functions - 仅使用 Dialogflow webhook 来存储聊天对话,为所有缩进调用 webhook,但使用意图中的默认文本作为响应
- python - 我是否需要在导入 numpy 或 tensorflow 的所有模块中设置种子?
- javascript - 如何解决数据表中的错误解析?
- r - 使用 R 中的标头调用 API
- node.js - 执行 http 请求时,firebase 函数返回 403
- java - 如何将盐与生成的密码进行比较?
- html - li 元素边框太大
- node.js - 节点版本升级 npm ERR!无法验证,需要:BASIC realm="Sonatype Nexus Repository Manager"
- flutter - 在颤振中过滤地理位置
- r - 如何在 R 中创建一个函数来创建列的子集?