How To Reverse Array In PHP

In this tutorial, we will show you how to reverse an array (one,two and multidemsional) in PHP using for loop, array_unshift(), array_reverse().

How to Reverse Array In PHP?

Here are some methods:

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.

Here’s an example:

<?php
$numbers = array(1, 2, 3, 4, 5);
$reversed = array_reverse($numbers);

print_r($reversed);
?>

Output:

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.

Here’s an example:

<?php
$numbers = array(1, 2, 3, 4, 5);
$reversed = array();

for ($i = count($numbers) - 1; $i >= 0; $i--) {
    $reversed[] = $numbers[$i];
}

print_r($reversed);
?>

Output:

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.

Here’s an example:

<?php
$numbers = array(1, 2, 3, 4, 5);

foreach ($numbers as $number) {
array_unshift($reversed, $number);
}

print_r($reversed);
?>

Output:

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

Conclusion

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

Recommended PHP Tutorials

AuthorDevendra Dode

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

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