Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • List of some useful array functions in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 561
    Comment on it

    Hello friends,

    This article demonstrates some helpful array functions which are used to remove duplicate values from an array, counting the number of occurrences of values, picking out one or more elements randomly from an array and to randomize the order of array elements. The list and  description of each function are given below :

     

    1. array_unique()

    array_unique() function used to remove duplicate elements from an array and it returns an array with unique values.

     

    Syntax : array_unique(array, sort_flags)

     

    array : Specifies an input array.(Required)

    sort_flags : Used to define sorting behaviour.(Optional)

     

    sort_flags can have the following values :

    SORT_STRING : Compare array values as strings.(default)
    SORT_REGULAR : Compare values normally without changing types.
    SORT_NUMERIC : Used for comparing arrays values numerically.
    SORT_LOCALE_STRING : Compare array elements as strings based on the current locale.

     

    Description : array_unique() function takes an array as input and remove the duplicate values from it. The function returns an array with unique values. If an array contains two or more same values, then first value will be kept and others are deleted from resultant array.

     

    array_unique() function first sorts the array values as strings, then it keeps the key of first value and left keys of other matching values. The comparison of array values takes place by using (===) equality operator.

     

    Result : This function returns the filtered array with unique values.

     

    Example :

    <?php
    $array = array("a" => "green", "b" => "red", "c" => "green", "d" => "red");
    $result = array_unique($array);
    echo "<pre>";
    print_r($result);
    echo "</pre>";
    ?>

    Output :

    Array
    (
        [a] => green
        [b] => red
    )

     

     

    2. array_count_values()

    array_count_values() function counts the number of occurrences of a value in the array.

     

    Syntax : array_count_values(array)

    array : Specifies an input array.

     

    Description : array_count_values() function counts the frequency of each values in array. The function takes an array as input and returns an associative array with values as keys and the frequency of their occurrences in array as values.

     

    Result : The function returns an associative array with array values as keys and the count of their occurrences in array as values.

     

    Note : The function generates an E_WARNING for an element except string or integer type.

     

    Example :

    <?php
    $array = array("a" => "green", "2" => 2, "c" => "green", "3" => 3, "e" => "red", "4" => 2);
    $result = array_count_values($array);
    echo "<pre>";print_r($result);echo "</pre>";
    ?>
    

    Output :

    Array
    (
        [green] => 2
        [2] => 2
        [3] => 1
        [red] => 1
    )

     

     

    3. array_rand()

    array_rand() function is used to pick one or more values randomly from an array. This function returns the indexes of randomly selected values.

     

    Syntax : array_rand(array, num_key)

    array : Specifies an input array.(Required)

    num_key : Specifies the number of random keys to return. Default value is 1 (if not specified).(Optional)

     

    Description :  array_rand() function takes an array as input and an optional argument num_key which defines the number of random keys to return. The function returns a single key for randomly selected value as default if num_key not specified. If num_key argument also passed with value more than 1, then function returns an array of keys.

     

    Result : Without optional argument array_rand() returns a random key of an array. With the optional argument, it returns a random key or an array of random keys based on the value supplied for optional argument.(1 = random key, >1 = array of random keys).

     

    Example : Without optional argument

    <?php
    $array = array("a" => "green", "2" => 2, "c" => "green", "3" => 3, "e" => "red", "4" => 2);
    $result = array_rand($array);
    echo $result;
    echo "<br>";
    echo $array[$result];
    ?>

    Output :

    a
    green

     

    Example : With optional argument

    <?php
    $array = array("a" => "green", "2" => 2, "c" => "green", "3" => 3, "e" => "red", "4" => 2);
    $result = array_rand($array, 3);
    echo "<pre>";print_r($result);
    foreach($result as $key){
     echo $array[$key];
     echo "<br>";
    }
    ?>

    Output :

    Array
    (
        [0] => a
        [1] => 2
        [2] => 4
    )
    green
    2
    2

     

     

    4. shuffle()

    shuffle() function randomly reset the order of elements in the array.

     

    Syntax : shuffle(array)

    array : Specifies an input array.(Required)

     

    Description : shuffle() function is used to randomize the order of elements of the input array. The function removed, previously assigned keys and assign new keys instead of reordering older keys.

     

    Result : This function returns TRUE on success and FALSE on failure.

     

    Example :

    <?php
    $array = array("a" => "green", "2" => 2, "c" => "green", "3" => 3, "e" => "red", "4" => 2);
    echo shuffle($array);
    echo "<pre>";print_r($array);echo "</pre>";
    ?>

    Output :

    1
    Array
    (
        [0] => green
        [1] => green
        [2] => 3
        [3] => 2
        [4] => 2
        [5] => red
    )

     

 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: