假设我有一个数组:
$fruits = array("apple", "apple", "apple", "banana", "banana", "strawberry");
我想将此数组转换为数组计数:
$count = array("apple" => 3, "banana" => 2, "strawberry" => 1);
最简单的方法是什么才能实现这个目标?
答案 0 :(得分:3)
试试这个:
$count = array_count_values($fruits)
答案 1 :(得分:3)
那里,
您可以使用array_count_values
<?php
$array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values($array));
?>
Array
(
[1] => 2
[hello] => 2
[world] => 1
)