How do i count same values in an array and store it to a variable?

后端 未结 3 875
名媛妹妹
名媛妹妹 2020-11-29 11:58
$items = explode(\',\',$product); // values is 4,2,4,2,2,4

$unique_items=array_unique($items); // gives me 4,2

What code should be next to give me

相关标签:
3条回答
  • 2020-11-29 12:49

    see: array_count_values

    Like:

    $occurences = array_count_values($items);
    print_r($occurences);
    

    Output:

    Array
    (
        [4] => 3
        [2] => 3
    )
    

    Usage:

    echo $occurences[4]; // outputs 3
    
    0 讨论(0)
  • 2020-11-29 12:50

    You're probably looking for array_count_values() function.

    0 讨论(0)
  • 2020-11-29 12:51

    check here : array_count_values() https://www.w3schools.com/php/showphp.asp?filename=demo_func_array_count_values

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