I have an array created out of a string in my website:
$items = '"1010" => "3", "1010" => "1", "1020" => "2", "1030" => "6"'; //string
$items_element = array(); //this is the created array
$items_bits = explode(",", $items);
foreach ($items_bits as $i) {
$i = str_replace('"', '', $i);
$i_bobs = explode(" => ", $i);
$items_element[$i_bobs[0]] = $i_bobs[1];
}
To get the results of this array, I use a foreach()
loop:
foreach($items_element as $item_id => $item_count){
echo $item_id.': '.$item_count.'</br>';
}
Right now, the result is coming up like this:
1010: 3
1010: 1
1020: 2
1030: 6
And I need all the duplicate keys to be displayed only once, but their values has to be summed with each others.
Example.:
1010: 4
1020: 2
1030: 6
How can I do something like this? I've tried it in many different ways but the truth is that I'm lost and don't know how to get this results. So I was hoping that someone could enlighten my mind here...