Skip to content

All topics  /  PHP

How to Reverse Array In PHP?

PHP ·

Hi Dev,

This tutorial is focused on how to reverse an array in PHP. you'll learn how to use array_reverse() function in PHP. We will use how to reverse the array using PHP. if you have a question about how to use the array_reverse() function using PHP then I will give a simple example with a solution.

Now, let's see the article on how to reverse an array using PHP. it's a simple example of how to use the array_reverse() function using PHP. you can understand the concept of how to use the array_reverse() function in PHP. if you have a question about how to reverse an array in PHP then I will give a simple example with a solution.

Example 1: One Dimensional Array Reverse In PHP


<?php
    //create an array
    $numericArray = array(10, 12, 15, 50, 42);
    //reverse array
    $resNumericArray = array_reverse($numericArray);

    
    print_r($resNumericArray);
?>

Output:

Array
(
    [0] => 42
    [1] => 50
    [2] => 15
    [3] => 12
    [4] => 10
)

Example 2: Associative Array Reverse In PHP

<?php
    //create an array
    $keyValueArray = array("a"=>"PHP","b"=>"MySQL","c"=>"JAVA");

    
    //reverse array
    $resKeyValueArray = array_reverse($keyValueArray);

  
    print_r($resKeyValueArray);
?>

Output:

Array 
( 
    [c] => JAVA
    [b] => MySQL
    [a] => PHP
)

I hope it could help you...