Skip to content

Instantly share code, notes, and snippets.

@phpfiddle
Created August 14, 2020 04:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpfiddle/17cb9379d3467e4122db488b451319d8 to your computer and use it in GitHub Desktop.
Save phpfiddle/17cb9379d3467e4122db488b451319d8 to your computer and use it in GitHub Desktop.
[ Posted by Ricky Aditya ] Count positive numbers or sum negative numbers
<?php
function countPositivesSumNegatives(array $numbers) {
$result = array();
$total = 0;
$i = 0;
if (!empty($numbers)) {
foreach($numbers as $number) {
if($number < 0) {
$total += $number;
} else {
$i++;
}
}
$result[] = $i;
$result[] = $total;
return $result;
}
return array();
}
print_r(countPositivesSumNegatives([1, 2, 3, 4, 5, 6, 7, 8, 9, 10, -11, -12, -13, -14, -15]));
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment