Is there a PHP function to count the number of times a value occurs within an array?

后端 未结 2 693

I need to count the number of times a value occurs in a given array.

For example:

$array = array(5, 5, 2, 1);

// 5 = 2 times
// 2 = 1 time
// 1 = 1          


        
相关标签:
2条回答
  • 2020-12-21 04:30

    Yes, it's called array_count_values().

    $array = array(5, 5, 2, 1);
    $counts = array_count_values($array); // Array(5 => 2, 2 => 1, 1 => 1)
    
    0 讨论(0)
  • 2020-12-21 04:42

    array_count_values

    0 讨论(0)
提交回复
热议问题