Reputation: 1214
Below are five arrays. The idea is to count all the values for matching keys. And keep the single appearances. All these should appear in a new array. So, 14855 should have value 6, and 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 a way to do this, and with the option that you can add more arrays later as well?
Hope anyone can help me with this brainteaser for me!
Upvotes: 1
Views: 89
Reputation: 47894
It doesn't benefit your scripting to declare your input arrays as individual arrays. Declare them as rows of a parent array so that you can iterate them.
For every key which is encountered while looping, you add its value to the stored key's value (or zero if not encountered before).
Code: (Demo)
$array = [
[14638 => 5, 14855 => 5],
[101 => 4, 10141 => 4, 1015 => 4, 1020 => 4, 10353 => 4, 1048 => 4, 10582 => 4, 1060 => 4, 10675 => 4, 1068 => 4, 1084 => 4, 1098 => 4],
[101 => 3, 10141 => 3, 602 => 3, 341 => 3, 3523 => 3, 922 => 3, 2099 => 3, 7305 => 3, 222 => 3, 537 => 3, 2792 => 3],
[10141 => 2, 1232 => 2, 10909 => 2, 129 => 2, 155 => 2, 0 => 2, 156 => 2],
[14855 => 1, 96 => 1, 120 => 1, 129 => 1, 155 => 1, 156 => 1]
];
$result = [];
foreach ($array as $row) {
foreach ($row as $k => $v) {
$result[$k] = ($result[$k] ?? 0) + $v;
}
}
var_export($result);
Upvotes: 1
Reputation: 33397
This was a challenged question, but at the same time a nasty one ;)
I enjoyed 3 hours works and it is done.
First of all to say your array in the question is not well formed.
This question can be for sure solved in many ways. This is an example of one approach to achieve the goal and demonstrate how to do it, so you are welcome to play with it and learn more.
I wrote some notes inside the code. You can expand this to handle unlimited arrays, but the sum calculation is only for 2 keys, if you have more then 2 keys, I will leave to you, as now you have the 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 for out for loop
$arrCount = count($arrMerge) - 1;
//Our new array that contain a copy of all keys and values with out doubles
$arrNew = array();
//Creating new array of all arrays
for ($i = 0; $i < $arrCount; $i ++)
{
foreach ($arrMerge[$i] as $key => $value)
{
//echo "Key: $key; Value: $value<br />\n";
$arrNew[$key] = $value;
}
}
//The algorithm
for ($i = 0; $i < $arrCount; $i ++)
{
echo "<b>Array " . $i . " comparing with : </b>";
for ($j = $arrCount; $j > $i; $j --)
{
echo "<br> Array " . $j . " : <br/>";
for ($k = 0; $k < count($arrMerge[$i]); $k ++)
{
$key1 = array_keys($arrMerge[$i]);
$value1 = array_values($arrMerge[$i]);
for ($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] . " and " . $key2[$l] . " are identical keys, ";
echo "we sum : " . $value1[$k] . " and " . $value2[$l] . "<br />";
//We update the new array with the summed values.
$arrNew[$key1[$k]] = $value2[$l] + $value1[$k];
}
}
}
}
echo "<br><br>";
}
// printing results
echo "<b>This our new array with sum values</b><br/>";
foreach ($arrNew as $key => $value)
{
echo "Key: $key; Value: $value<br />\n";
}
?>
It is tested and here is the output of this code, The first part is a list of matching keys and last part as you can see the key 14855 got value 6 and key 101 got value 7 etc.:
Comparing all 5 Arrays Keys output
Array 0 comparing with :
Array 4 :
match found: 14855 and 14855 are identical keys, we sum : 5 and 1
Array 3 :
Array 2 :
Array 1 :
Array 1 comparing with :
Array 4 :
Array 3 :
match found: 10141 and 10141 are identical keys, we sum : 4 and 2
Array 2 :
match found: 101 and 101 are identical keys, we sum : 4 and 3
match found: 10141 and 10141 are identical keys, we sum : 4 and 3
Array 2 comparing with :
Array 4 :
Array 3 :
match found: 10141 and 10141 are identical keys, we sum : 3 and 2
Array 3 comparing with :
Array 4 :
match found: 129 and 129 are identical keys, we sum : 2 and 1
match found: 155 and 155 are identical keys, we sum : 2 and 1
match found: 156 and 156 are identical keys, we sum : 2 and 1
Last part our new Array with sum of each Key
This our new array with 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
Upvotes: 1