5

Hi I have 2 arrays like

 array(a) {  [0]=> array(2) { ["count"]=> string(2) "22" ["hour"]=> string(1) "0" }
             [1]=> array(2) { ["count"]=> string(2) "17" ["hour"]=> string(1) "1" } 
             [2]=> array(2) { ["count"]=> string(2) "22" ["hour"]=> string(1) "2" } 
             [3]=> array(2) { ["count"]=> string(2) "15" ["hour"]=> string(1) "3" } 
             [4]=> array(2) { ["count"]=> string(2) "15" ["hour"]=> string(1) "4" }
            }

And I have the second array with different (count) values

 array(b) {  [0]=> array(2) { ["count"]=> string(2) "12" ["hour"]=> string(1) "0" }
             [1]=> array(2) { ["count"]=> string(2) "36" ["hour"]=> string(1) "1" } 
             [2]=> array(2) { ["count"]=> string(2) "59" ["hour"]=> string(1) "2" } 
             [3]=> array(2) { ["count"]=> string(2) "5"  ["hour"]=> string(1) "3" } 
             [4]=> array(2) { ["count"]=> string(2) "27" ["hour"]=> string(1) "4" }
            }

Can you please tell me how can I get the sum of ["count"]s of both arrays and get a new array like

 array(c) {  [0]=> array(2) { ["count"]=> string(2) "34" ["hour"]=> string(1) "0" }
             [1]=> array(2) { ["count"]=> string(2) "53" ["hour"]=> string(1) "1" } 
             [2]=> array(2) { ["count"]=> string(2) "81" ["hour"]=> string(1) "2" } 
             [3]=> array(2) { ["count"]=> string(2) "20"  ["hour"]=> string(1) "3" } 
             [4]=> array(2) { ["count"]=> string(2) "42" ["hour"]=> string(1) "4" }
            }

Thank you.

  • What do you have so far? – jeroen Feb 18 '13 at 17:47
  • check out the array functions in the php manual. `array_map()` is a good place to start. http://php.net/array_map – dnagirl Feb 18 '13 at 17:50
  • he means that you don't get answers for free. Your question is pretty straightforward, so google and the PHP manual should be your first stop. You want our effort; show us yours. – dnagirl Feb 18 '13 at 17:51
  • What have you tried? I was going to write that we are not here to do your work for you but it seems that that is not the case... – jeroen Feb 18 '13 at 17:52
  • [merge/sum multi dimentional array php](https://stackoverflow.com/q/44607229/6521116) – LF00 Jun 17 '17 at 17:55

4 Answers4

2
$result_array = array() ;

for ($i = 0, $length = count($a_array) ; $i < $length ; $i++){
  $result_array[$i] = $a_array[$i] ;
  $result_array[$i]["count"] += $b_array[$i]['count'] ;  
}

var_dump($result_array) ;
sybear
  • 7,837
  • 1
  • 22
  • 38
0

Try this

$count1 = array_count_values($array1);

$count1 = array_count_values($array2);

$count = $count1 + $count2;
Naveen D Almeida
  • 877
  • 6
  • 16
0
$sum = array();
foreach($array1 as $k=>$v) {
  foreach($array2 as $k1=>$v1) {
    if($v['hour'] == $v1['hour']) {
      $sum[] = array('count'=> ($v['count'] == $v1['count']), 'hour' => $v['hour']);
    }
  }
}

Should give you a new array, $sum in your specified format. My assumptions are that your first 2 arrays are called $array1 and $array2. Hope that helps.

Husman
  • 6,819
  • 9
  • 29
  • 47
0
//new array to hold answers
$array_results=array();

//loop through first array
foreach ($array_first as $key=>$v){

//use key of current element to get corresponding value from second array
//add them together and add into combined array
$array_results[]=$v['count']+$array_second[$key]['count'];

}
cosmicsafari
  • 3,949
  • 11
  • 37
  • 56