Skip to content

Instantly share code, notes, and snippets.

@maxmillernunes
Created January 3, 2020 15:09
Show Gist options
  • Save maxmillernunes/975eb6e94c5f779faa6fa738915e04cd to your computer and use it in GitHub Desktop.
Save maxmillernunes/975eb6e94c5f779faa6fa738915e04cd to your computer and use it in GitHub Desktop.
<?php
function combineAndSumUp ($myArray = []) {
$finalArray = Array ();
foreach ($myArray as $nkey => $nvalue) {
$has = false;
$fk = false;
// Remove comma from numbers
$nvalue['iccid'] = str_replace(",","",$nvalue["iccid"]);
foreach ($finalArray as $fkey => $fvalue) {
if ( ($fvalue['iccid'] == $nvalue['iccid']) ) {
$has = true;
$fk = $fkey;
break;
}
}
if ( $has === false ) {
$finalArray[] = $nvalue;
} else {
$finalArray[$fk]['valor'] += $nvalue['valor'];
}
}
return $finalArray;
}
$d = array(
array
(
"iccid" => "89551094253001026895",
"valor" => 15
),
array
(
"iccid" => "89551094253001026895",
"valor" => 30
)
);
$r = combineAndSumUp($d);
print_r($r);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment