Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to count the number of occurrences of values in an array

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 125
    Comment on it

    There is a function array_count_values() in PHP which counts the frequency of occurrences of all values of an array. The function returns an associative array with original values as keys and the number of occurrences as values.

    Syntax: array_count_values(array)
    Description of function parameters :
    array: An array of values to count (Required)

    Example:

    <?php
    $array = array(1, "test", 1, 'a', "test", 'a', 1, "hello");
    echo "<pre>";
    print_r(array_count_values($array));
    echo "</pre>";
    ?>
    

    Output:

    Array
    (
        [1] => 3
        [test] => 2
        [a] => 2
        [hello] => 1
    )
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: