php数组根据字段值统计出现次数
文章描述:
$array = [ [‘name’ => ‘apple’, ‘color’ => […]
$array = [
['name' => 'apple', 'color' => 'red'],
['name' => 'banana', 'color' => 'yellow'],
['name' => 'apple', 'color' => 'red'],
['name' => 'orange', 'color' => 'orange'],
['name' => 'banana', 'color' => 'yellow'],
['name' => 'apple', 'color' => 'red'],
];
// 提取数组中'color'字段的值
$colors = array_column($array, 'color');
// 统计各个颜色出现的次数
$count = array_count_values($colors);
print_r($count);
Array
(
[red] => 3
[yellow] => 2
[orange] => 1
)
发布时间:2024/12/23
发表评论