php - Sum of all values on key with different arrays -


below 5 arrays. idea count values matching keys. , keep single appearances. these should appear in new array. so, 14855 should have value 6, , 101 value 7 etc.

$arr_one = array ( [14638] => 5 [14855] => 5 )  $arr_two = array ( [101] => 4 [10141] => 4 [1015] => 4 [1020] => 4 [10353] => 4 [1048] => 4 [10582] => 4 [1060] => 4 [10675] => 4 [1068] => 4 [1084] => 4 [1098] => 4 $arr_three = array ( [101] => 3 [10141] => 3 [602] => 3 [341] => 3 [3523] => 3 [922] => 3 [2099] => 3 [7305] => 3 [222] => 3 [537] => 3 [2792] => 3 $arr_four = array ( [10141] => 2 [1232] => 2 [10909] => 2 [129] => 2 [155] => 2 [] => 2 [156] => 2 $arr_five = array ( [14855] => 1 [96] => 1 [120] => 1 [129] => 1 [155] => 1 [156] => 1  

is there way this, , option can add more arrays later well?

hope can me brainteaser me!

this challenged question, @ same time nasty 1 ;)

i enjoyed 3 hours works , done.

first of array in question not formed.

this question can sure solved in many ways. example of 1 approach achieve goal , demonstrate how it, welcome play , learn more.

i wrote notes inside code. can expand handle unlimited arrays, sum calculation 2 keys, if have more 2 keys, leave you, have concept.

<?php  $arr1 = [14638 => 5, 14855 => 5]; $arr2 = [101 => 4, 10141 => 4, 1015 => 4, 1020 => 4, 10353 => 4, 1048 => 4, 10582 => 4, 1060 => 4, 10675 => 4, 1068 => 4, 1084 => 4, 1098 => 4]; $arr3 = [101 => 3, 10141 => 3, 602 => 3, 341 => 3, 3523 => 3, 922 => 3, 2099 => 3, 7305 => 3, 222 => 3, 537 => 3, 2792 => 3]; $arr4 = [10141 => 2, 1232 => 2, 10909 => 2, 129 => 2, 155 => 2, 0 => 2, 156 => 2]; $arr5 = [14855 => 1, 96 => 1, 120 => 1, 129 => 1, 155 => 1, 156 => 1];  //we merge arrays $arrmerge = array($arr1, $arr2, $arr3, $arr4, $arr5);  //count how many keys in merged array subtract 1 out loop $arrcount = count($arrmerge) - 1;  //our new array contain copy of keys , values out doubles $arrnew = array();  //creating new array of arrays ($i = 0; $i < $arrcount; $i ++) {     foreach ($arrmerge[$i] $key => $value)     {         //echo "key: $key; value: $value<br />\n";         $arrnew[$key] = $value;     } }  //the algorithm ($i = 0; $i < $arrcount; $i ++) {     echo "<b>array " . $i . " comparing : </b>";     ($j = $arrcount; $j > $i; $j --)     {         echo "<br> array " . $j . " : <br/>";          ($k = 0; $k < count($arrmerge[$i]); $k ++)         {             $key1 = array_keys($arrmerge[$i]);             $value1 = array_values($arrmerge[$i]);             ($l = 0; $l < count($arrmerge[$j]); $l ++)             {                 $key2 = array_keys($arrmerge[$j]);                 $value2 = array_values($arrmerge[$j]);                 if ($key1[$k] == $key2[$l])                 {                     echo "match found: " . $key1[$k] . " , " . $key2[$l] . " identical keys, ";                     echo "we sum : " . $value1[$k] . " , " . $value2[$l] . "<br />";                     //we update new array summed values.                     $arrnew[$key1[$k]] = $value2[$l] + $value1[$k];                 }             }          }     }     echo "<br><br>"; }  // printing results echo "<b>this our new array sum values</b><br/>"; foreach ($arrnew $key => $value) {     echo "key: $key; value: $value<br />\n"; }  ?> 

it tested , here output of code, first part list of matching keys , last part can see key 14855 got value 6 , key 101 got value 7 etc.:

comparing 5 arrays keys output

array 0 comparing : array 4 : match found: 14855 , 14855 identical keys, sum : 5 , 1 array 3 : array 2 : array 1 :  array 1 comparing : array 4 :     array 3 : match found: 10141 , 10141 identical keys, sum : 4 , 2 array 2 : match found: 101 , 101 identical keys, sum : 4 , 3 match found: 10141 , 10141 identical keys, sum : 4 , 3  array 2 comparing : array 4 : array 3 : match found: 10141 , 10141 identical keys, sum : 3 , 2  array 3 comparing : array 4 : match found: 129 , 129 identical keys, sum : 2 , 1 match found: 155 , 155 identical keys, sum : 2 , 1 match found: 156 , 156 identical keys, sum : 2 , 1 

last part our new array sum of each key

this our new array sum values key: 14638; value: 5 key: 14855; value: 6 key: 101; value: 7 key: 10141; value: 5 key: 1015; value: 4 key: 1020; value: 4 key: 10353; value: 4 key: 1048; value: 4 key: 10582; value: 4 key: 1060; value: 4 key: 10675; value: 4 key: 1068; value: 4 key: 1084; value: 4 key: 1098; value: 4 key: 602; value: 3 key: 341; value: 3 key: 3523; value: 3 key: 922; value: 3 key: 2099; value: 3 key: 7305; value: 3 key: 222; value: 3 key: 537; value: 3 key: 2792; value: 3 key: 1232; value: 2 key: 10909; value: 2 key: 129; value: 3 key: 155; value: 3 key: 0; value: 2 key: 156; value: 3 

Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -