I need filter out duplicate rows in my 2d array and in the retained unique rows append an element that contains the count of how many times the unique row existed in the original array.
I wanted to use array_unique($array, SORT_REGULAR)
, but removing duplicates is not enough -- I actually need to get store the count of the duplicated rows per with the unique rows.
I have tried array_search()
and loops, but none of my attempts yield the correct results. My project data has upwards of 500,000 entries, but here's a basic example:
Input:
[
['manufacturer' => 'KInd', 'brand' => 'ABC', 'used' => 'true'],
['manufacturer' => 'KInd', 'brand' => 'ABC', 'used' => 'true'],
['manufacturer' => 'KInd', 'brand' => 'ABC', 'used' => 'false'],
]
Output:
[
['manufacturer' => 'KInd', 'brand' => 'ABC', 'used' => 'true', 'count' => 2],
['manufacturer' => 'KInd', 'brand' => 'ABC', 'used' => 'false', 'count' => 1],
]