I am trying to combine / summarize 2 arrays that can contain integers or more arrays (which contain an integer).
When the values ββare integers, I need to sum them in a finite array. When the values ββare arrays, I need to go through the values ββand sum them in the final array.
If a value or sub-matrix exists in only 1 base array, it must be added to the sub-matrix of the final array. (This is something that I canβt do) ..)
My arrays are as follows:
ARRAY 1 [1466859600] => Array ( [TOTAL] => 27217 [AAA] => Array ( [FD_CDP] => 1746 [LO_SC_MIC] => 4654 [FD_ATS] => 893 [CDP] => 40 [SUPERVISION] => 9 [CONTROL] => 4 [ATS] => 4 [EVT_ACK] => 3 ) [BBB] => Array ( [FD_CDP] => 1376 [LO_SC_MIC] => 4606 [FD_ATS] => 826 [FD_ATSS] => 451 [LO_SFRC] => 4 [FD_S2] => 259 [2_LOSC] => 2 ) [CCC] => Array ( [FD_CDP] => 1333 [LO_SC_MIC] => 4725 [FD_ATS] => 856 [CONTROL] => 4 [ATS] => 2 [EVT_ACK] => 5 ) ARRAY 2 [1466859600] => Array ( [TOTAL] => 95406 [AAA] => Array ( [FD_ATSS] => 1719 [LO_SC_MIC] => 16830 [CONTROL] => 16 [NEW] => 7 [NOEL] => 206 ) [BBB] => Array ( [SUPERVISION] => 23 [CDP] => 158 [CONTROL] => 40 [2_LOSC] => 14 [ATS] => 6 [EVT_ACK] => 4 ) [CCC] => Array ( [EVT_ACK] => 167 [LO_SFRC] => 248 [SUPERVISION] => 23 )
I wrote a function like this:
function sumArrayValues($array1, $array2) { foreach ($array1 as $key => $value) { if (is_array($array1[$key])) { echo "it an array\n I need to reloop\n"; sumArrayValues($array1[$key], $array2[$key]); } else { echo "FIRST VALUE TO SUM\n"; print_r($array1[$key]."\n"); echo "SECOND VALUE TO SUM\n"; print_r($array2[$key]."\n"); $array1[$key] = (int)$array1[$key] +(int)$array2[$key]; echo "--------RESULT of SUM array1&2----------\n"; } } return $array1; }
But this function does not take into account 2 (and possibly more) cases: if the submatrix is ββnot in the same order, if the sub-array or value exists only in the second array.
An example function would be good help, but on a more fundamental level, I can't even figure out an algorithm for this. Any ideas?
arrays php
remi legend Jun 17 '17 at 17:06 on 2017-06-17 17:06
source share