diff --git a/src/Math/minBy.php b/src/Math/minBy.php new file mode 100644 index 0000000..4e37445 --- /dev/null +++ b/src/Math/minBy.php @@ -0,0 +1,55 @@ + + * @copyright Copyright (c) 2018 + */ + +namespace _; + +use function _\internal\baseIteratee; + +/** + * This method is like `min` except that it accepts `iteratee` which is + * invoked for each element in `array` to generate the criterion by which + * the value is ranked. The iteratee is invoked with one argument: (value). + * + * @category Math + * + * @param array $array The array to iterate over. + * @param callable|string $iteratee The iteratee invoked per element. + * + * @return mixed Returns the minimum value. + * @example + * + * $objects = [['n' => 1], ['n' => 2]]; + * + * minBy($objects, function($o) { return $o['n']; }); + * // => ['n' => 1] + * + * // The `property` iteratee shorthand. + * minBy($objects, 'n'); + * // => ['n' => 1] + * + */ +function minBy(?array $array, $iteratee) +{ + $iteratee = baseIteratee($iteratee); + $result = null; + $computed = null; + + foreach ($array as $key => $value) { + $current = $iteratee($value); + + if (null !== $current && (null === $computed ? ($current === $current) : $current < $computed)) { + $computed = $current; + $result = $value; + } + } + + return $result; +} pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy