Ciaran
Ciaran

Reputation: 695

How to reverse an array in php WITHOUT using the array reverse method

I want to know how to reverse an array without using the array_reverse method. I have an array called reverse array which is the one i want to reverse. My code is below. could someone point out what i am doing wrong as I cannot find any example of reversing an array this way anywhere else. my code is below.

<?php

//Task 21 reverse array

$reverseArray = array(1, 2, 3, 4);
$tmpArray = array();
$arraySize = sizeof($reverseArray);

for($i<arraySize; $i=0; $i--){
    echo $reverseArray($i);
}

?>

Upvotes: 11

Views: 32173

Answers (6)

Anupam Verma
Anupam Verma

Reputation: 69

Reverse an array in php WITHOUT using any function the array reverse method

<?php
  $array = [11,22,32,42,52,62,71];
   $count = sizeof($array);
   for($i = $count -1;$i >=0;$i--){
     $data[] = $array[$i];
   }
   
   print_r($data);
  
?> 

Upvotes: 0

slevy1
slevy1

Reputation: 3832

Here's another way in which I borrow the code from here and update it so that I eliminate having to use a $temp variable and instead take advantage of array destructuring, as follows:

<?php
/* Function to reverse  
$arr from start to end*/
function reverseArray(&$arr, $start,  
                       $end) 
{ 
    while ($start < $end) 
    { 
        [$arr[$start],$arr[$end]] = [$arr[$end],$arr[$start]];

        $start++; 
        $end--; 
    }  
}      

$a = [1,2,3,4];
reverseArray($a,0,count($a)-1);
print_r($a);

See live code

One of the advantages of this technique is that the array $a is reversed in place so there is no necessity of creating a new array.

The following improves the code so that one need not pass more than one parameter to reverseArray(); as well as indicating that there is no return value:

/* Function to reverse  
$arr from start to end*/
function reverseArray(&$arr, $start=0): void 
{ 
    $end = count($arr)-1;
    while ($start < $end) 
    { 
        [$arr[$start],$arr[$end]] = [$arr[$end],$arr[$start]];

        $start++; 
        $end--; 
    }  
}      

$a = [1,2,3,4];
reverseArray($a);
print_r($a);

See here

Note: this revision works in PHP 7.2-7.4.2

Upvotes: 0

Kiss Kopp&#225;ny
Kiss Kopp&#225;ny

Reputation: 950

The problem with your method is when you reach 0, it runs once more and index gets the value of -1.

$reverseArray = array(1, 2, 3, 4);
$arraySize = sizeof($reverseArray);

for($i=$arraySize-1; $i>=0; $i--){
    echo $reverseArray[$i];
}

Upvotes: 0

Sai AmarNath Chintha
Sai AmarNath Chintha

Reputation: 13

Below is the code to reverse an array, The goal here is to provide with an optimal solution. Compared to the approved solution above, my solution only iterates for half a length times. though it is O(n) times. It can make a solution little faster when dealing with huge arrays.

<?php
  $ar = [34, 54, 92, 453];
  $len=count($ar);
  for($i=0;$i<$len/2;$i++){

    $temp = $ar[$i];
    $ar[$i] = $ar[$len-$i-1];
    $ar[$len-$i-1] = $temp;
  }

  print_r($ar)

?>

Upvotes: 1

mring17
mring17

Reputation: 1

how to reverse a array without using any predefined functions in php.. i had a solution for this problem... here is my solution........

<?php
// normal array --------
$myarray = [1,2,3,4,5,6,7,8,9];
//----------------
    $arr = [];
    for($i=9; $i > -1; $i--){

        if(!$i==0){

            $arr[]= $i;
        }   
    }
    print_r($arr);



//the out put is [9,8,7,6,5,4,3,2,1];
?>

Upvotes: -2

Robert
Robert

Reputation: 5662

<?php
  $array = array(1, 2, 3, 4);
  $size = sizeof($array);

  for($i=$size-1; $i>=0; $i--){
      echo $array[$i];
  }
?>

Upvotes: 16

Related Questions