• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / Reversing an Array in php with array_reverse() Function

Reversing an Array in php with array_reverse() Function

March 24, 2022 Leave a Comment

In php, you can reverse the items in an array easiest with the array_reverse() function.

$example = array("lion", "bear", "snake", "horse");

$reversed = array_reverse($example);

print_r($reversed);

//Output:
Array
(
    [0] => horse
    [1] => snake
    [2] => bear
    [3] => lion
)

When working with arrays and collections of data in php, it is useful to be able to easily change and manipulate our data structures easily.

One such operation is to be able to reverse an array.

To reverse an array in php, we can use the php array_reverse() function.

array_reverse() takes an array and returns the reverse of the given array.

Below is an example of how to use array_reverse() to reverse an array in php.

$example = array("lion", "bear", "snake", "horse");

$reversed = array_reverse($example);

print_r($reversed);

//Output:
Array
(
    [0] => horse
    [1] => snake
    [2] => bear
    [3] => lion
)

Preserving the Keys when Reversing an Array in php with array_reverse()

When reversing an array, by default, the numeric keys are updated. For example, above, we had an array of 4 items. After reversing, the item that previously was last, and had a key value of ‘3’, had a key value of ‘0’.

You can pass a second argument to array_reverse() to preserve the numeric keys after reversing the array.

Just pass ‘true’ after the array you want to reverse in your call to array_reverse() to preserve the keys.

Below is an example of how to use array_reverse() to reverse an array and preserve the numeric keys in php.

$example = array("lion", "bear", "snake", "horse");

$reversed = array_reverse($example, true);

print_r($reversed);

//Output:
Array
(
    [3] => horse
    [2] => snake
    [1] => bear
    [0] => lion
)

How to Reverse an Array without array_reverse() in php

If you want to reverse an array without a function in php, you can also use a loop.

To use a for loop for reversing an array without the reverse() function, we will swap items in the list in the following way.

First, we swap the first and last items. Next, we continue with swapping the second and second to last items, then the third and third to last items, until we reach the middle of the list.

Below is an example of how to use a loop to reverse an array in php.

$example = array("lion", "bear", "snake", "horse");

foreach(range(0,count($example)/2) as $i) {
    [$example[$i], $example[count($example) - $i - 1]]= [$example[count($example) - $i - 1],$example[$i]];
}

print_r($example);

//Output:
Array
(
    [0] => horse
    [1] => snake
    [2] => bear
    [3] => lion
)

Hopefully this article has been useful for you to learn how to use php to reverse arrays.

Other Articles You'll Also Like:

  • 1.  php max – Find Maximum Value of Array in php
  • 2.  php trim() Function – Remove Whitespace at Beginning and End of String
  • 3.  php atan – Find Arctangent and Inverse Tangent of Number
  • 4.  php array_slice() – Get Slice of Elements from Array
  • 5.  php array_intersect() – Find Intersection of Two or More Arrays in php
  • 6.  php is_array() Function – Check if Variable is Array in php
  • 7.  php Convert Radians to Degrees with php rad2deg() Function
  • 8.  Get Last Day of Month in php
  • 9.  Get Last Element of Array in php
  • 10.  php Absolute Value with abs Function

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy