首页 > 解决方案 > 检查多维数组的所有值是否为空

问题描述

我有一个这样的多维数组,我不想检查所有“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();
        }
    }

先感谢您,

标签: phplaravelmultidimensional-arrayisnullis-empty

解决方案


如果包含的所有值都为空,则使用将返回 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)

推荐阅读