如何检查是否一个多维数组只包含空值? [英] How to check if a multi-dimensional array only contains empty values?

查看:155
本文介绍了如何检查是否一个多维数组只包含空值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,不能完全找到这个答案,所以我不知道我是否包含数组像这样的..

I looked around and can't quite find the answer for this, so I'm wondering if I contain an array such as this..

$array['foo']['bar'][1] = '';
$array['foo']['bar'][2] = '';
$array['foo']['bar'][3] = '';
$array['foo']['bar'][4] = '';

我如何检查是否所有的值都是空的?我试着做以下几点:

How can I check if all the values are empty? I tried doing the following:

if (empty($array['foo']['bar'])) {
    // Array empty
}

但如预期,没有工作。

But as expected that didn't work.

我怎样才能做到这一点?

How can I do this?

推荐答案

如果你想检查是否所有在这里居住,你可以使用值

If you wanted to check to see if all of the values where populated you can use

 if(call_user_func_array("isset", $array['foo']['bar']))

有关你想做的事,虽然你可以使用数组减少与封闭是什么

For what you want to do though you could use array reduce with a closure

 if(array_reduce($array, function(&$res, $a){if ($a) $res = true;}))

请注意,这只会在PHP 5.3工作+

Note this will only work in php 5.3+

这篇关于如何检查是否一个多维数组只包含空值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆