Reversing an array in php

array_reverse

Takes an input array and returns a new array with the order of the elements reversed.

Parameters

If set to true numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.

Return Values

Returns the reversed array.

Examples

Example #1 array_reverse() example

$input = array( «php» , 4.0 , array( «green» , «red» ));
$reversed = array_reverse ( $input );
$preserved = array_reverse ( $input , true );

print_r ( $input );
print_r ( $reversed );
print_r ( $preserved );
?>

The above example will output:

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

See Also

User Contributed Notes

  • Array Functions
    • array_​change_​key_​case
    • array_​chunk
    • array_​column
    • array_​combine
    • array_​count_​values
    • array_​diff_​assoc
    • array_​diff_​key
    • array_​diff_​uassoc
    • array_​diff_​ukey
    • array_​diff
    • array_​fill_​keys
    • array_​fill
    • array_​filter
    • array_​flip
    • array_​intersect_​assoc
    • array_​intersect_​key
    • array_​intersect_​uassoc
    • array_​intersect_​ukey
    • array_​intersect
    • array_​is_​list
    • array_​key_​exists
    • array_​key_​first
    • array_​key_​last
    • array_​keys
    • array_​map
    • array_​merge_​recursive
    • array_​merge
    • array_​multisort
    • array_​pad
    • array_​pop
    • array_​product
    • array_​push
    • array_​rand
    • array_​reduce
    • array_​replace_​recursive
    • array_​replace
    • array_​reverse
    • array_​search
    • array_​shift
    • array_​slice
    • array_​splice
    • array_​sum
    • array_​udiff_​assoc
    • array_​udiff_​uassoc
    • array_​udiff
    • array_​uintersect_​assoc
    • array_​uintersect_​uassoc
    • array_​uintersect
    • array_​unique
    • array_​unshift
    • array_​values
    • array_​walk_​recursive
    • array_​walk
    • array
    • arsort
    • asort
    • compact
    • count
    • current
    • end
    • extract
    • in_​array
    • key_​exists
    • key
    • krsort
    • ksort
    • list
    • natcasesort
    • natsort
    • next
    • pos
    • prev
    • range
    • reset
    • rsort
    • shuffle
    • sizeof
    • sort
    • uasort
    • uksort
    • usort
    • each

    Источник

    PHP array_reverse() Function

    The array_reverse() function returns an array in the reverse order.

    Syntax

    Parameter Values

    Technical Details

    Return Value: Returns the reversed array
    PHP Version: 4+
    PHP Changelog: The preserve parameter was added in PHP 4.0.3

    More Examples

    Example

    Return the original array, the reversed array and the preserved array:

    Unlock Full Access 50% off

    COLOR PICKER

    colorpicker

    Join our Bootcamp!

    Report Error

    If you want to report an error, or if you want to make a suggestion, do not hesitate to send us an e-mail:

    Thank You For Helping Us!

    Your message has been sent to W3Schools.

    Top Tutorials
    Top References
    Top Examples
    Get Certified

    W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

    Источник

    How To Reverse Array In PHP

    Reversing an array is a common operation in PHP, and there are several methods available to achieve it. Reversing an array essentially means changing the order of its elements so that the last element becomes the first, the second-to-last element becomes the second, and so on. This can be useful for a variety of purposes, such as displaying the elements of an array in reverse order, or implementing certain algorithms that require the elements of an array to be processed in reverse order.

    One of the simplest and most efficient methods to reverse an array in PHP is by using the array_reverse() function. This function takes an array as input and returns a new array with the elements in reverse order. Another method involves using a for loop to iterate over the original array in reverse order and adding each element to a new array. Alternatively, the array_unshift() function can be used to insert the elements of the original array at the beginning of a new array in reverse order.

    Each of these methods has its own advantages and disadvantages, and the choice of method depends on the specific needs of the project. It is important to carefully consider the performance, readability, and maintainability of the code when selecting a method to reverse an array in PHP. With the right approach, reversing an array can be a simple and straightforward operation that allows you to process and manipulate arrays with ease.

    How To Reverse Array In PHP

    • Method 1: Using the array_reverse() Function
    • Method 2: Using a For Loop
    • Method 3: Using the array_unshift() Function

    Method 1: Using the array_reverse() Function

    The simplest way to reverse an array in PHP is by using the built-in array_reverse() function. This function accepts an array as an argument and returns a new array with the elements in reverse order.

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

    As you can see, the array_reverse() function returns a new array with the elements in reverse order.

    Method 2: Using a For Loop

    Another way to reverse an array in PHP is by using a for loop. This method involves creating a new array and iterating over the original array in reverse order, adding each element to the new array.

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

    In this example, you first create an empty array $reversed . Then, you iterate over the original array $numbers in reverse order using a for loop. For each iteration, you add the current element to the $reversed array using the array [] syntax. Finally, you print the $reversed array using the print_r() function.

    Method 3: Using the array_unshift() Function

    A third method to reverse an array in PHP is by using the array_unshift() function. This function accepts an array as the first argument and one or more values as the remaining arguments. The function inserts the specified values at the beginning of the array, effectively reversing the order of elements.

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

    In this example, you first create an empty array $reversed . Then, you iterate over the original array $numbers using a foreach loop. For each iteration, you insert the current element at the beginning of the $reversed array using the array_unshift() function. Finally, you print the $reversed array using the print_r() function.

    Conclusion

    In this article, you have learned three different methods to reverse an array in PHP.

    Источник

    PHP: array_reverse() function

    PHP: Create an array with elements in reverse order

    The array_reverse() function is used to reverse the order of the elements in an array.

    array_reverse(array_name, preserve_keys)
    Name Description Required /
    Optional
    Type
    array_name Specifies the name of the array. Required Array
    preserve_keys Specify TRUE or FALSE whether function shall preserve the array’s keys or not. The default value is FALSE. Optional Boolean

    Return value:

    Value Type: Array

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

    Pictorial Presentation:

    php function reference: array_reverse() function

    Practice here online :

    Previous: array_reduce
    Next: array_search

    Follow us on Facebook and Twitter for latest update.

    PHP: Tips of the Day

    Best way to find this is: create a php file and add the following code:

    and open it in browser, it will show the file which is actually being read!

    • Weekly Trends
    • Java Basic Programming Exercises
    • SQL Subqueries
    • Adventureworks Database Exercises
    • C# Sharp Basic Exercises
    • SQL COUNT() with distinct
    • JavaScript String Exercises
    • JavaScript HTML Form Validation
    • Java Collection Exercises
    • SQL COUNT() function
    • SQL Inner Join
    • JavaScript functions Exercises
    • Python Tutorial
    • Python Array Exercises
    • SQL Cross Join
    • C# Sharp Array Exercises

    We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook

    Источник

    How to Use array_reverse() Function in PHP

    While working with arrays in PHP, we may need to reverse the order of an array. For this, we have a predefined PHP function called array_reverse(). This function allows us to reverse the order of any input array. This article covers the array_reverse() function in detail, including its syntax, parameters, and example codes.

    What is the array_reverse() Function in PHP?

    The array_reverse() function can reverse or reorder the array elements. This function takes an array as its input and returns a new array with the order of the elements reversed.

    Syntax

    The syntax for the array_reverse() function is as follows:

    The function takes an array as its first argument and an optional Boolean value as its second argument. If the second parameter preserve_keys is set to TRUE, the function will preserve the keys of the array. If the second parameter is not set or set to FALSE, the function will reset the keys of the array. By default, a FALSE value is assigned to this parameter.

    Parameter

    The array_reverse() function takes two parameters:

    • Array: The array to be reversed. This is a required parameter.
    • preserve_keys: An optional Boolean value that determines whether to preserve the keys of the array or not. It is set to FALSE by default.

    Return

    A reversed array is returned by array_reverse() function.

    Example 1: Reversing an Array

    In the code given below, we will create an array of numbers and then use the array_reverse() function to reverse the order of the elements in the array without preserving the keys:

    $numbers = array ( 1 , 2 , 3 , 4 , 5 ) ;
    $reversed_numbers = array_reverse ( $numbers ) ;
    print_r ( $reversed_numbers ) ;
    ?>

    Output
    In the output, we can see the array_reverse() function has reversed the order of the elements in the array without preserving the keys:

    Example 2: Reversing an Array and Preserving Keys

    Now we will create an array of strings and then using the array_reverse() function we will reverse the order of the elements in the array while preserving the keys:

    $numbers = array ( 1 , 2 , 3 , 4 , 5 ) ;
    $reversed_numbers = array_reverse ( $numbers , True ) ;
    print_r ( $reversed_numbers ) ;
    ?>

    Output
    Here, the array_reverse() function has reversed the order of the elements in the array while preserving the keys:

    Example 3: Reversing a Multidimensional Array

    In the code below we have created a multidimensional array and then using the array_reverse() we will reverse the order of array elements:

    $animals = array (
    array ( ‘name’ => ‘dog’ , ‘color’ => ‘brown’ ) ,
    array ( ‘name’ => ‘cat’ , ‘color’ => ‘gray’ ) ,
    array ( ‘name’ => ‘bird’ , ‘color’ => ‘blue’ )
    ) ;
    $reversed_animals = array_reverse ( $animals ) ;
    print_r ( $reversed_animals ) ;
    ?>

    Output
    In the output, we can see the array_reverse() function has reversed the order of elements present inside a multidimensional array:

    Array (
    [ 0 ] => Array ( [ name ] => bird [ color ] => blue )
    [ 1 ] => Array ( [ name ] => cat [ color ] => gray )
    [ 2 ] => Array ( [ name ] => dog [ color ] => brown )
    )

    Conclusion

    With array_reverse() in PHP, we can reorder the elements of an array. A new array is returned by this function, with the elements rearranged according to the input array. This article covers the details of array_reverse() function. For a complete description of the syntax, parameters, and return value of this function read the article.

    About the author

    Kashif

    I am an Electrical Engineer. I love to write about electronics. I am passionate about writing and sharing new ideas related to emerging technologies in the field of electronics.

    Источник

    Читайте также:  Css селектор input name
Оцените статью