Last active
January 28, 2023 02:35
-
-
Save phpdever/ce203fea48afebb17d47e5d5da98d785 to your computer and use it in GitHub Desktop.
php 数组去重计数
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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