Matt Toner
Matt Toner

Reputation: 246

PHP Arrays - Merge two arrays where a value is added together

I need some more help regarding PHP Arrays and the issue I am having. I have an array like this: -

array(2) {
  [0]=>
  array(2) {
    [0]=>
    array(2) {
      ["count"]=>
      string(3) "100"
      ["id"]=>
      int(46)
    }
    [1]=>
    array(2) {
      ["count"]=>
      string(3) "300"
      ["id"]=>
      int(53)
    }
  }
  [1]=>
  array(1) {
    [0]=>
    array(2) {
      ["count"]=>
      string(3) "200"
      ["id"]=>
      int(46)
    }
  }
}

However, I would like it to look more like this as array: -

array(2) {
    [0]=>
    array(2) {
      ["count"]=>
      string(3) "300" <--- This has been added from Array 1 and 2
      ["id"]=>
      int(46)
    }
    [1]=>
    array(2) {
      ["count"]=>
      string(3) "300"
      ["id"]=>
      int(53)
    }
  }

Basically if the same id is in both areas I want the count number to be added to each other but if it's not then it needs to just be left alone and included in the array.

I have used a number of array functions such as array_merge and array_push but I am running out of ideas of how this could work. I have also started working on a foreach with if statements but I just got myself completely confused. I just need a second pair of eyes to look at the issue and see howe it can be done.

Thanks again everyone.

Upvotes: 1

Views: 75

Answers (3)

moni_dragu
moni_dragu

Reputation: 1163

You could use something like this:

$end_array = array();

function build_end_array($item, $key){

    global $end_array;

    if (is_array($item)){

        if( isset($item["id"])){

            if(isset($end_array[$item["id"]]))
                $end_array[$item["id"]] = $end_array[$item["id"]] + $item["count"]*1;
            else 
                $end_array[$item["id"]] = $item["count"]*1;
        }
        else {
            array_walk($item, 'build_end_array');
        }
    }
}

array_walk($start_array, 'build_end_array');

Here is a fiddle.

Upvotes: 1

Matt Toner
Matt Toner

Reputation: 246

Thank you ever so much everyone. I actually worked it by doing this: -

$fullArray = array_merge($live, $archive);

$array = array();
foreach($fullArray as $key=>$value) {
    $id = $value['id'];
    $array[$id][] = $value['count'];
}

$result = array();
foreach($array as $key=>$value) {
    $result[] = array('id' => $key, 'count' => array_sum($value));
}

return $result;

Upvotes: 0

NiMeDia
NiMeDia

Reputation: 1004

Should work with something like this:

$idToCountArray = array(); //temporary array to store id => countSum
array_walk_recursive($inputArray, function($value,$key) { //walk each array in data structure
    if(isset(value['id']) && isset($value['count'])) {
        //we have found an entry with id and count:
        if(!isset($idToCountArray[$value['id']])) {
            //first count for id => create initial count
            $idToCountArray[$value['id']] = intval($value['count']);
        } else {
            //n'th count for id => add count to sum
            $idToCountArray[$value['id']] += intval($value['count']);
        }
    }
});
//build final structure:
$result = array();
foreach($idToCountArray as $id => $countSum) {
    $result[] = array('id' => $id, 'count' => ''.$countSum);
}

Please note that I have not testet the code and there is probably a more elegant/performant solution.

Upvotes: 1

Related Questions