I have a large array with several objects, where there are several duplicates for them. I got an unique array using array_unique(PHP). Now, I need to know how to get how manny times each object was there in the original array?
I have tried array_search, and loops but nothing got the correct results. Some what similar array like here, but it's very large set, aroud 500K entries.
[{
"manufacturer": "KInd",
"brand": "ABC",
"used": "true"
},
{
"manufacturer": "KInd",
"brand": "ABC",
"used": "true"
},
{
"manufacturer": "KInd",
"brand": "ABC",
"used": "false"
}]
You don't need to use any elaborate serialization or encoding to create composite keys for grouping. Just implode each row's values (assuming they all contain the same columns in the same order) to create an identifying key for the result array.
On the first encounter, store the row's data in the group and set the group's count to 1; on any subsequent encounter, increment the group's counter.
Code: (Demo)
$result = [];
foreach ($array as $row) {
$compositeKey = implode('_', $row);
if (!isset($result[$compositeKey])) {
$result[$compositeKey] = $row + ['count' => 1];
} else {
++$result[$compositeKey]['count'];
}
}
var_export(array_values($result));
Output:
array (
0 =>
array (
'manufacturer' => 'KInd',
'brand' => 'ABC',
'used' => 'true',
'count' => 2,
),
1 =>
array (
'manufacturer' => 'KInd',
'brand' => 'ABC',
'used' => 'false',
'count' => 1,
),
)
Other posts that leverage multiple identifying column values for grouping:
If I understand you correctly, this should help
function getUniqWithCounts(array $data): array
{
$result = [];
foreach ($data as $item) {
$hash = md5(serialize($item));
if (isset($result[$hash])) {
$result[$hash]['count']++;
continue;
}
$item['count'] = 1;
$result[$hash] = $item;
}
return array_values($result);
}
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Find the answer in similar questions on our website.
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.
PHP (from the English Hypertext Preprocessor - hypertext preprocessor) is a scripting programming language for developing web applications. Supported by most hosting providers, it is one of the most popular tools for creating dynamic websites.
The PHP scripting language has gained wide popularity due to its processing speed, simplicity, cross-platform, functionality and distribution of source codes under its own license.
https://www.php.net/
Welcome to the Q&A site for web developers. Here you can ask a question about the problem you are facing and get answers from other experts. We have created a user-friendly interface so that you can quickly and free of charge ask a question about a web programming problem. We also invite other experts to join our community and help other members who ask questions. In addition, you can use our search for questions with a solution.
Ask about the real problem you are facing. Describe in detail what you are doing and what you want to achieve.
Our goal is to create a strong community in which everyone will support each other. If you find a question and know the answer to it, help others with your knowledge.