加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP函数验证数组

发布时间:2020-12-13 22:00:57 所属栏目:PHP教程 来源:网络整理
导读:是否有一个函数,我可以给一个数组,如果提供的函数为所有函数返回true,它将返回true? theFunction(array(1,2,3),'is_numeric') //truetheFunction(array(1,"b",'is_numeric') //false 解决方法 不,但你可以使用array_reduce: array_reduce(array(1,function
是否有一个函数,我可以给一个数组,如果提供的函数为所有函数返回true,它将返回true?

theFunction(array(1,2,3),'is_numeric') //true
theFunction(array(1,"b",'is_numeric') //false

解决方法

不,但你可以使用array_reduce:

array_reduce(array(1,function ($a,$v) { return $a && is_numeric($v); },true);

您当然可以构建自己的高阶函数:

function for_all(array $arr,$func) {
    return array_reduce($arr,$v) use ($func) {
            return $a && call_user_func($func,$v);
        },true);
}

var_dump(
    for_all(array(1,'is_numeric')
); //true

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读