I've this array actually:
Array
(
[0] => Array
(
[color] => Red
[count] => 6
)
[1] => Array
(
[color] => Purple
[count] => 6
)
[2] => Array
(
[color] => Red
[count] => 6
)
)
How can i create a new array with the sum by color like this:
|--------|-------|
| Color | Count |
|--------|-------|
| Red | 12 |
| Purple | 6 |
|--------|-------|
This is what I tried without success:
array_sum(array_map(
function($item) {
return $item['color'];
}, $items)
);
What I'm missing here please ?
Thanks a lot for your help.
array_mapwill remove thecountand you only get a plain array with color strings, that cannot be summed. You could use a simple loop to build a color map and then adding the counts manually to that single map.+=to add to last value inside loop to get total count for the colours into a new array