简体   繁体   中英

Is there a PHP equivalent of JavaScript's Array.prototype.some() function

In JavaScript, we can do:

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

Is there a PHP equivalent of the some() function?

No, there is no short circuiting equivalent in the PHP standard library. There are any number of non-short circuiting solutions, among which array_reduce would probably fit best:

var_dump(array_reduce([2, 5, 8, 1, 4], function ($isBigger, $num) {
    return $isBigger || $num > 10;
}));

It may be worth implementing your own some / any / all functions, or use a library which provides a collection of functional programming primitives like this, eg https://github.com/lstrojny/functional-php .

It is not included, but they are easily created. This uses the SRFI-1 names any and every but can be renamed some and 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;
}
function array_some(array $array, callable $callback) {

        $i = 0;
        $n = count($array);

        while($i<$n && !$callback($array[$i])) {
            $i++;
        }

        return $array[$i] ?? null;
}

The function returns null if no element is found that satisfies the condition of the "callback" function.

If it finds an element that satisfies the condition, it will return the first one it finds.

The "callback" function that is passed as a parameter must return true or false depending on whether or not the element satisfies the condition.

There's array_filter(), which returns a subset of the given array based on the return value of the given callback. If the subset is empty then it would be the equivalent of Some() returning false, and if it's not empty then that would match Some() returning 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));

This approach doesn't short-circuit, however, and the performance will be inversely proportional to the size of the array. This shouldn't matter in the real world, though, because the array will still have to get pretty huge, or the array_filter gets called a lot of times before you'll notice an impact on performance.

If performance is paramount then you'll have to loop the array yourself and break out of the loop as soon as you find a match.

$biggerThanTen = false;
foreach ($unfiltered as $elem)
{
    if ($elem > 10)
    {
        $biggerThanTen = true;
        break;
    }
}

Use array_filter and provide a callback. Wrap this in another function to count whether there are any results

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM