在JavaScript中,我们可以做:
function isBiggerThan10(element, index, array) {
return element > 10;
}
[2, 5, 8, 1, 4].some(isBiggerThan10); // false
[12, 5, 8, 1, 4].some(isBiggerThan10); // true
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some
有类似于some((函数的PHP吗?
它不包括在内,但它们很容易创建。这使用SRFI-1名称any
和every
,但可以重命名为some
和all
:
function array_any(array $array, callable $fn) {
foreach ($array as $value) {
if($fn($value)) {
return true;
}
}
return false;
}
function array_every(array $array, callable $fn) {
foreach ($array as $value) {
if(!$fn($value)) {
return false;
}
}
return true;
}
否,PHP标准库中没有短路等效项。有许多非短路解决方案,其中array_reduce
可能最适合:
var_dump(array_reduce([2, 5, 8, 1, 4], function ($isBigger, $num) {
return $isBigger || $num > 10;
}));
实现您自己的some
/any
/all
函数可能是值得的,或者使用一个库来提供类似这样的函数编程原语集合,例如。https://github.com/lstrojny/functional-php.
有array_filter((,它根据给定回调的返回值返回给定数组的子集。如果子集为空,那么它将相当于Some((返回false,如果它不为空,则它将与Some(((返回true相匹配。
$unfiltered = [1, 11, 2, 22, 3, 33, 4, 44, 5, 55];
$filtered = array_filter ($unfiltered, function ($elem){
return $elem > 10;
});
print_r ($unfiltered);
print_r ($filtered);
var_dump (empty ($filtered));
然而,这种方法不会短路,性能将与阵列的大小成反比。不过,这在现实世界中并不重要,因为数组仍然必须变得非常大,或者array_filter被调用了很多次,然后才会注意到对性能的影响。
如果性能至关重要,那么您必须自己循环数组,并在找到匹配项后立即脱离循环。
$biggerThanTen = false;
foreach ($unfiltered as $elem)
{
if ($elem > 10)
{
$biggerThanTen = true;
break;
}
}
function array_some(array $array, callable $callback) {
$i = 0;
$n = count($array);
while($i<$n && !$callback($array[$i])) {
$i++;
}
return $array[$i] ?? null;
}
如果没有找到满足"0"条件的元素,则函数返回null;回调";作用
如果它找到一个满足条件的元素,它将返回它找到的第一个元素。
";回调";作为参数传递的函数必须返回true或false,具体取决于元素是否满足条件。
这里有一个PHP函数,它的工作原理与JavaScriptArray.some((函数类似。
我相信代码是不言自明的,但如果你有任何问题,请随时问我。
// JavaScript-like Array.some() function
$array_some = function($an_array, $callback_function) {
$is_data_found = false;
foreach ($an_array as $an_array_index => $array_item) {
$is_data_found = $callback_function($array_item, $an_array_index, $an_array);
if ($is_data_found) return $is_data_found;
}
return $is_data_found;
};
在普通数组中的用法:
$numbers = [12, 34, 27, 23, 65, 93, 36, 87, 4, 254];
$is_any_number_less_than500 = $array_some($numbers, fn($number) => $number < 500) === false ? 'false' : 'true';
echo("<pre>is any number < 500: $is_any_number_less_than500</pre><br />");
// is any number < 500: true
$is_any_number_more_than500 = $array_some($numbers, fn($number) => $number > 500) === false ? 'false' : 'true';
echo("<pre>is any number > 500: $is_any_number_more_than500</pre><br /><br />");
// is any number > 500: false
在关联数组中的用法:
(类似JavaScript的对象数组/类似JavaScript的JSON(
$products = [
['id' => 'id_1', 'price' => 30],
['id' => 'id_2', 'price' => 233],
['id' => 'id_3', 'price' => 5],
['id' => 'id_4', 'price' => 499]
];
$is_any_product_price_less_than500 = $array_some($products, fn($product) => @$product['price'] < 500) === false ? 'false' : 'true';
echo("<pre>is any product price < 500: $is_any_product_price_less_than500</pre><br />");
// is any product price < 500: true
$is_any_product_price_more_than500 = $array_some($products, fn($product) => @$product['price'] > 500) === false ? 'false' : 'true';
echo("<pre>is any product price > 500: $is_any_product_price_more_than500</pre><br /><br />");
// is any product price > 500: true
使用OP数据:
$is_any_number_bigger_than10 = $array_some([2, 5, 8, 1, 4], fn($number) => $number > 10) === false ? 'false' : 'true';
echo("<pre>is any number > 10: $is_any_number_bigger_than10</pre><br />");
// is any number > 10: false
$is_any_number_bigger_than10 = $array_some([12, 5, 8, 1, 4], fn($number) => $number > 10) === false ? 'false' : 'true';
echo("<pre>is any number > 10: $is_any_number_bigger_than10</pre><br /><br />");
// is any number > 10: true
使用array_filter并提供回调。将其封装在另一个函数中,以计数是否有任何结果
function array_some(array $data, callable $callback) {
$result = array_filter($data, $callback);
return count($result) > 0;
}
$myarray = [2, 5, 8, 12, 4];
array_some($myarray, function($value) {
return $value > 10;
}); // true