arrayutils
Search
K

findIndex()

The findIndex() method all similar to find(), but return index
Example.php
<?php use kim\present\utils\arrays\ArrayUtils;
$arrayUtils = ArrayUtils::from(["Apple", "Banana", "Carrot", "Bacon"]);
//Find value's index ​​starting with B
$arrayUtils->findIndex(function($name){ return $name[0] === "B"; });
// expected output: 1

Syntax

$arrayUtils->find(callable $callback) : mixed;

Parameter

  • $callback
    A function to execute on each value in the array, taking 3 arguments:
    • $value The current element being processed in the array.
    • $key The index of the current element being processed in the array.
    • $array The array every was called upon.

Return value

  • The key of the first element in the array that pass function. Otherwise, NULL is returned.

Prefixing

ArrayUtils::findFrom(iterable $from, callable $callback) : mixed;

References

Last modified 2yr ago