I have two arrays the structure is exactly the same only the values are different, like this..
// $array1
Array
(
[Backlight] => Array
(
[Supply_Type] => Backlight
[Value] => 100.00
[Count] => 2
)
[Body Glass] => Array
(
[Supply_Type] => Body Glass
[Value] => 100.00
[Count] => 82
)
)
// $array2
Array
(
[Backlight] => Array
(
[Supply_Type] => Backlight
[Value] => 200.00
[Count] => 1
)
[Body Glass] => Array
(
[Supply_Type] => Body Glass
[Value] => 150.00
[Count] => 90
)
)
The goal is to create one array from both with the highest value from "Value" and "Count" for each type ( Body Glass, Backlight ).
So it should look like this...
// $finalArray
Array
(
[Backlight] => Array
(
[Supply_Type] => Backlight
[Value] => 200.00
[Count] => 2
)
[Body Glass] => Array
(
[Supply_Type] => Body Glass
[Value] => 150.00
[Count] => 90
)
)
As there could be more than two arrays I was thinking I could do this with array_uintersect but as far as i'm aware, through trial and error, this only returns values from one array and compares against the rest, and feel that maybe i'm going down the wrong path.
I just thought I would add that this.
The problem started when using array_reduce, I needed to add up the values from two values ("value" and "count") in an array, ideally I wanted to just write...
$sum = array_reduce($array, function ($a, $b) use($key) {
isset($a[$b[$key]]) ? $a[$b[$key]]["Value"] += $b["Value"] : $a[$b[$key]] = $b;
isset($a[$b[$key]]) ? $a[$b[$key]]["Count"] += $b["Count"] : $a[$b[$key]] = $b;
return $a;
});
but the second assignment didnt add up correctly, it was adding the first array value twice, if I remember correctly.
So I settled for this which creates two or more arrays...
foreach($values as $value) {
$sum[] = array_reduce($array, function ($a, $b) use($key, $value) {
isset($a[$b[$key]]) ? $a[$b[$key]][$value] += $b[$value] : $a[$b[$key]] = $b;
return $a;
});
}
And also brings me to this current issue.