Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions src/ArrayHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -687,4 +687,32 @@ public static function mergeRecursive(...$args): array

return $result;
}

/**
* Returns an array with numeric values only.
*
* @param array $array The original unfiltered.
* @param bool $onlyPositive Mode for filtering only positive numbers.
*
* @return array Filtered array.
*
* @since 2.0.0
* @throws \InvalidArgumentException
*/
public static function filterNumeric(array $array, $onlyPositive = true): array
{
$result = [];

foreach ($array as $i => $val) {
$type = gettype($val);
$val = trim($val);

if (is_numeric($val) && (!$onlyPositive || $val >= 0)) {
settype($val, $type);
$result[$i] = $val;
}
}

return $result;
}
}