Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use array_reverse() in PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 136
    Comment on it

    array_reverse() function reset the order of elements in an array in reverse order. The function takes an array as argument and return an array containing all values in reverse order. The function also reset the index of elements. To keep the indexes same we have to pass one more optional argument with values true or false.


    Syntax : array_reverse(array, preserve_keys)


    array : Specifies an input array.(Required)
    preserve_keys : Optional parameter can be set TRUE or FALSE.(Optional)


    Note :

    • If value of preserve keys are set to TRUE, The function will preserve the keys. Default value is FALSE.
    • Non-numeric keys are always preserved.

    Example :

    <?php
        $arr= array(1, 5, 7, 10, 14);  
        $arr1 = array_reverse($arr);
        $preserve = array_reverse($arr,true);    
        print_r($arr1);    
        print_r($preserve);  
    ?>
    

    Output :
    Array ( [0] => 14 [1] => 10 [2] => 7 [3] => 5 [4] => 1 )
    Array ( [4] => 14 [3] => 10 [2] => 7 [1] => 5 [0] => 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: