Duplicate values multi array
Posted
by
BETA911
on Stack Overflow
See other posts from Stack Overflow
or by BETA911
Published on 2013-10-17T14:25:17Z
Indexed on
2013/10/17
15:55 UTC
Read the original article
Hit count: 77
As the title states I'm searching for a unique solution in multi arrays. PHP is not my world so I can't make up a good and fast solution. I basically get this from the database: http://pastebin.com/vYhFCuYw .
I want to check on the 'id' key, and if the array contains a duplicate 'id', then the 'aantal' should be added to each other.
So basically the output has to be this: http://pastebin.com/0TXRrwLs .
Thanks in advance!
EDIT As asked, attempt 1 out of many:
function checkDuplicates($array) {
$temp = array();
foreach($array as $k) {
foreach ($array as $v) {
$t_id = $k['id'];
$t_naam = $k['naam'];
$t_percentage = $k['percentage'];
$t_aantal = $k['aantal'];
if ($k['id'] == $v['id']) {
$t_aantal += $k['aantal'];
array_push($temp, array(
'id' => $t_id,
'naam' => $t_naam,
'percentage' => $t_percentage,
'aantal' => $t_aantal,
)
);
}
}
}
return $temp;
}
© Stack Overflow or respective owner