1

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...

Community
  • 1
  • 1
user3050478
  • 272
  • 3
  • 19
  • Possible duplicate of [How to merge two arrays by summing the merged values](http://stackoverflow.com/questions/6086267/how-to-merge-two-arrays-by-summing-the-merged-values) – H2ONOCK Dec 07 '16 at 15:23
  • Thanks for your comment @H2ONOCK, I've seen a couple of questions similar to that one, but it was difficult for me to put in practice since the array's format were different in that example. – user3050478 Dec 07 '16 at 15:31

2 Answers2

2

Change your loop as follows:

foreach ($items_bits as $i) {
    $i = str_replace('"', '', $i);
    $i_bobs = explode(" => ", $i);
    if (!isset($items_element[trim($i_bobs[0])]))
        $items_element[trim($i_bobs[0])] = 0;
    $items_element[trim($i_bobs[0])] += (int)$i_bobs[1];
}

trim() is added, because the second index of your array started with space (e.g. " 1010" instead of "1010"). That's also why you got duplicates in resulting array. If it weren't the case, the key would be overwritten by the last occurrence.

Jirka Hrazdil
  • 3,983
  • 1
  • 14
  • 17
  • I tested it here and it worked perfectly! Thanks. Forgive me for my lack of knowledge as I'm starting in the php language, so the function `trim()` is to remove spaces of strings? – user3050478 Dec 07 '16 at 15:28
  • 1
    @user3050478: Indeed, `trim()` removes whitespace characters (such as space, tab, new line, ...) from the beginning and the end of the string. See [documentation](http://php.net/trim) for more information. – Jirka Hrazdil Dec 07 '16 at 15:30
  • 1
    Well thank you so much @Jiri Hrazdil, you have no idea how much that served to me, not just to finish those lines of code, but I actually learned something out of it! – user3050478 Dec 07 '16 at 15:33
1
$items = '"1010" => "3", "1010" => "1", "1020" => "2", "1030" => "6"'; //string
$items_element = array(); //this is the created array
$items_bits = explode(",", preg_replace('/\s+/', '', $items));

foreach($items_bits as $value) {
    $value_exploded = explode('=>', $value);

    $value_exploded_key = (int)str_replace('"', '', $value_exploded[0]);
    $value_exploded_value = (int)str_replace('"', '', $value_exploded[1]);
    $value_exploded_value = !isset($items_element[$value_exploded_key]) ? $value_exploded_value : $items_element[$value_exploded_key] + $value_exploded_value;

    $items_element[$value_exploded_key] = $value_exploded_value;
}

var_dump($items_element);
Wolverine
  • 1,712
  • 1
  • 15
  • 18
  • Thanks for sharing your method @Perumal93, I just quickly tested it and I could not notice any errors. I'll still use the first one though, since it was already applied to my codes and it looks quite simplier. – user3050478 Dec 07 '16 at 16:36
  • Yeah. The first one looks simpler. Even though it is so, I've shared my approach. I've used ternary expression rather than going for `if else` statements. Maybe that one makes it bit complicated for you. – Wolverine Dec 07 '16 at 16:38