I have a multidimensional array, in which I want to count similar occurrences.
So basically I want this:
[
[
'type' => 'frosties',
'madeby' => 'kelloggs'
],
[
'type' => 'frosties',
'madeby' => 'kelloggs'
],
[
'type' => 'cornflakes',
'madeby' => 'kelloggs'
]
];
To end out as this:
[
[
'type' => 'frosties',
'madeby' => 'kelloggs',
'count' => 2
],
[
'type' => 'cornflakes',
'madeby' => 'kelloggs',
'count' => 1
]
]
This is what I've come up with so far:
public function count($array) {
$newArr = [];
foreach ($array as $breakfast) {
if (in_array($breakfast['type'], $newArr) && in_array($breakfast['madeby'], $newArr)) {
//what goes here?
//dosomething['count']++;
} else {
$newArr[] = [
'type' => $breakfast['type'],
'madeby' => $breakfast['madeby'],
'count' => 0
];
}
}
return $newArr;
}
I might have been staring at this for too long, but I just can't seem to come up with what goes inside the if().