Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 633
Rory Avatar asked Oct 05 '16 13:10

Rory


People also ask

Can I use array prototype at ()?

Array.prototype.at() The at() method takes an integer value and returns the item at that index, allowing for positive and negative integers. Negative integers count back from the last item in the array.

What is array method in PHP?

An array in PHP is actually an ordered map. A map is a type that associates values to keys. This type is optimized for several different uses; it can be treated as an array, list (vector), hash table (an implementation of a map), dictionary, collection, stack, queue, and probably more.

What is prototype in array prototype?

prototype allows you to add new properties and methods to arrays. prototype is a property available with all JavaScript objects.

What is array prototype map in JavaScript?

Array.prototype.map() The map() method creates a new array populated with the results of calling a provided function on every element in the calling array.


1 Answers

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; } 
like image 115
Sylwester Avatar answered Sep 20 '22 03:09

Sylwester