Skip to content

Instantly share code, notes, and snippets.

@phpdever
Last active January 28, 2023 02:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save phpdever/ce203fea48afebb17d47e5d5da98d785 to your computer and use it in GitHub Desktop.
Save phpdever/ce203fea48afebb17d47e5d5da98d785 to your computer and use it in GitHub Desktop.
php 数组去重计数
<?php
// 原始数组
$primitiveArray = [
[1, 2, ''],
[2, ''],
[2, ''],
[2, 4, ''],
[1, 2, 3, ''],
[2, 3, ''],
];
// 计数数组
$countedData = [];
foreach ($primitiveArray as $arr) {
foreach ($arr as $value) {
if (empty($value)) {
continue;
}
if (!isset($countedData[$value])) {
$countedData[$value] = ['counts' => 0, 'name' => $value];
}
$countedData[$value]['counts']++;
}
}
print_r(array_values($countedData));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment