11

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);
}

?>

6 Answers 6

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

  for($i=$size-1; $i>=0; $i--){
      echo $array[$i];
  }
?>
Sign up to request clarification or add additional context in comments.

1 Comment

What if I have to store reverse array it $array it self?
1

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)

?>

Comments

0

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];
}

Comments

0

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

1 Comment

@Deadpool: If you click the "live code" link that i provide you will note that this code runs perfectly fine in PHP 7.1+. Given that the inferior versions of PHP 5.4.0 - 7.0.33do not support the array destructuring feature, the results of course produce parse errors, not FATAL ERROR messages. That outcome holds true in general for the other inferior versions of PHP 5 and PHP 4.. – slevy1 11 mins ago Delete
0

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);
  
?> 

Comments

-2

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];
?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.