Skip to content

Commit 1131c20

Browse files
committed
Implement find(), findKey(), any() and all() methods
1 parent 6402803 commit 1131c20

File tree

1 file changed

+68
-0
lines changed

1 file changed

+68
-0
lines changed

src/ArrayHelper.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1436,4 +1436,72 @@ private static function parseMixedPath(array|float|int|string $path, string $del
14361436

14371437
return is_string($path) ? StringHelper::parsePath($path, $delimiter) : $path;
14381438
}
1439+
1440+
/**
1441+
* @param array $array The array that should be searched
1442+
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, the value is returned from `find()` and the callback will not be called for further elements
1443+
*
1444+
* @return mixed Returns the value of the first element for which the `$predicate` callback returns true. If no matching element is found the function returns `null`
1445+
*/
1446+
public static function find(array $array, Closure $predicate): mixed
1447+
{
1448+
foreach ($array as $key => $value) {
1449+
if ($predicate($value, $key)) {
1450+
return $value;
1451+
}
1452+
}
1453+
1454+
return null;
1455+
}
1456+
1457+
/**
1458+
* @param array The array that should be searched
1459+
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, the key is returned from `findKey()` and the callback will not be called for further elements
1460+
*
1461+
* @return string|int|null Returns the key of the first element for which the `$predicate` callback returns `true`. If no matching element is found the function returns `null`
1462+
*/
1463+
public static function findKey(array $array, Closure $predicate): string|int|null
1464+
{
1465+
foreach ($array as $key => $value) {
1466+
if ($predicate($value, $key)) {
1467+
return $key;
1468+
}
1469+
}
1470+
1471+
return null;
1472+
}
1473+
1474+
/**
1475+
* @param array The array that should be searched
1476+
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns truthy value, `true` is returned from `any()` and the callback will not be called for further elements
1477+
*
1478+
* @return bool Returns `true`, if one element for which predicate callback returns truthy value. Otherwise the function returns `false`
1479+
*/
1480+
public static function any(array $array, Closure $predicate): bool
1481+
{
1482+
foreach ($array as $key => $value) {
1483+
if ($predicate($value, $key)) {
1484+
return true;
1485+
}
1486+
}
1487+
1488+
return false;
1489+
}
1490+
1491+
/**
1492+
* @param array The array that should be searched
1493+
* @param Closure $predicate The predicate callback to call to check each element. The first parameter contains the value, the second parameter contains the corresponding key. If this function returns falsy value, `false` is returned from `all()` and the callback will not be called for further elements
1494+
*
1495+
* @return bool Returns `false`, if one element for which predicate callback returns truthy value. Otherwise the function returns `false`
1496+
*/
1497+
public static function all(array $array, Closure $predicate): bool
1498+
{
1499+
foreach ($array as $key => $value) {
1500+
if (!$predicate($value, $key)) {
1501+
return false;
1502+
}
1503+
}
1504+
1505+
return true;
1506+
}
14391507
}

0 commit comments

Comments
 (0)