How To Use Array Reverse Function With Example In PHP

admin_img Posted By Bajarangi soft , Posted On 22-09-2020

In PHP we reverse array elements using array_reverse() function and array_reverse returns an array in the reverse order.

PHP array reverse Function with an example

Syntax PHP array_reverse() Function.

array_reverse(array, preserve)
 

Parameter   Description

array        Specifies an array
preserve     Specifies if the function should preserve the keys of the array or not. Possible values:
                true
                false
 

Example(1)

<?php
$array=array("a"=>"Bike1","b"=>"Bike2","c"=>"Bike3");
print_r(array_reverse($array));
?>

In above example we created new array and we reverse that array using array_reverse() Function


Complete code for PHP array_reverse() Function.
<!DOCTYPE html>
<html>
<head>
    <title>how to use PHP array_reverse() Function</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <br>
    <br>
    <br>
    <div class="text-center">
        <h1>PHP array_reverse() Function</h1>
    </div>
    <br>
    <br>
    <h2></h2>
    <div class="well">
        <?php
        $array=array("a"=>"Bike1","b"=>"Bike2","c"=>"Bike3");
        print_r(array_reverse($array));
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post