find()
The find() method find the value of the first element that pass function.
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot", "Bacon"]);
//Find values starting with B
$arrayUtils->find(function($name){ return $name[0] === "B"; });
// expected output: "Banana"Syntax
$arrayUtils->find(callable $callback) : mixed;Parameter
$callbackA function to execute on each value in the array, taking 3 arguments:
$valueThe current element being processed in the array.$keyThe index of the current element being processed in the array.$arrayThe arrayeverywas called upon.
Return value
The value of the first element in the array that pass function. Otherwise,
NULLis returned.
Prefixing
ArrayUtils::findFrom(iterable $from, callable $callback) : mixed;References
Last updated