PHP等价的一些(有用的)函数式编程函数,如any(),all()?

时间:2011-10-03 16:10:05

标签: php

// any() should return true if any of the element is true
$satisfied = any(array(x > 0, true, isset(y)));

请不要为自己编写函数而烦恼:我知道怎么做,我只是好奇它们是否已经存在于PHP中了!

5 个答案:

答案 0 :(得分:2)

Ouzo goodies库包含Arrays :: all和Arrays :: any。

例如,对于用户数组,假设类User具有isCool方法:

$isAnyCool = Arrays::any($users, Functions::extract()->isCool());

还有其他有用的类/函数用于函数式编程。查看: http://ouzo.readthedocs.org/en/latest/documentation/functional_programming.html

答案 1 :(得分:1)

答案 2 :(得分:1)

看看http://php.net/in_array

<?php
$arraydef = array(x > 0, true, isset(y));
$satisfied = in_array(true, $arraydef);
?>

答案 3 :(得分:0)

array_filter()可以用作any()

的一个非常有用的替代品

我不知道all()的一个(假设所有数组元素都为true时它应该返回true)

答案 4 :(得分:0)

Non-standard PHP library (NSPL)提供了allany以及许多其他有用的功能。