Created
January 3, 2020 15:09
-
-
Save maxmillernunes/975eb6e94c5f779faa6fa738915e04cd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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