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?
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.
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.
prototype allows you to add new properties and methods to arrays. prototype is a property available with all JavaScript objects.
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.
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; }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With