-2

I have tried something like that but it's giving me value. I need all the elements reverse in array again.

 <?php
//There is an array 
  echo '<pre>';
  $arr = ['a','b','c','d','e','f'];
  print_r($arr);
  $size = sizeof($arr);
 //print_r($size);

 for($i=5; $i<=$size; $i--)
 {
  echo $arr[$i];
 }

?>

but it gives me output "fedcba" I need like this ["f", "e", "d", "c", "b", "a"] reverse all of its elements without using any PHP array function and any other new variable.

halfer
  • 19,824
  • 17
  • 99
  • 186
Anand Choudhary
  • 566
  • 4
  • 16
  • What does your function return – Rotimi Sep 27 '17 at 08:39
  • Firstly pop out elements from array . Then again push individual elements to array in reverse format – Tavish Aggarwal Sep 27 '17 at 08:40
  • Do you need to reverse the array, or can a new reversed array be accepted? – Andreas Sep 27 '17 at 08:40
  • I need to reverse the array – Anand Choudhary Sep 27 '17 at 08:42
  • You can't do that without overwriting the current values. You will need a temporary array (or string) to hold the values – Andreas Sep 27 '17 at 08:45
  • Without any other variables it will be impossible. You have to use a counter or something to keep track of things. – Andreas Sep 27 '17 at 08:48
  • This old post could be for your help. https://stackoverflow.com/questions/30610523/reverse-array-in-javascript-without-mutating-original-array – Umesh Kumar Sharma Sep 27 '17 at 09:08
  • Why you need this `without using any PHP function and any other variable` ? Even [count()](http://php.net/manual/en/function.count.php) and [unset](http://php.net/manual/en/function.unset.php) is a function – Niklesh Raut Sep 27 '17 at 09:28
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Sep 27 '17 at 10:22

7 Answers7

3

Expanding Hanky Panky's answer If you don't need any additional element then you can unset the previous values of array.

    <?php
    $arr = ['a','b','c','d','e','f'];

    for($i=count($arr)-1;$i>=0;$i--){
    $arr[]=$arr[$i];
    unset($arr[$i]);
    }
    print_r($arr);
   //Additionaly you can use array_values to reindex your array from 0
    print_r(array_values($arr));
    ?>

Fiddle is Check here

Saad Suri
  • 1,352
  • 1
  • 14
  • 26
  • `$arr[]=$arr[$i];` in this line is `$arr` new blank array (new object)? – Anand Choudhary Sep 27 '17 at 08:58
  • 2
    It is not creating new object It is assigning values to your previous array. $arr is not a new object and blank array It is a continuation of your previous array – Saad Suri Sep 27 '17 at 09:01
0

//Use this

$arr = ['a','b','c','d','e','f'];

$size = sizeof($arr);
    $res=array();
  for($i=$size-1; $i>=0; $i--){
       $res[]= $arr[$i];
  }

print_r($res);
Pawan Thakur
  • 591
  • 8
  • 19
0

This uses the same amount of variables as your code meaning $arr, $size and $i.

$arr = ['a','b','c','d','e','f'];

$size = count($arr);
for($i=$size-1;$i>=0;$i--){
    $arr[$size] = $arr[$i];
    unset($arr[$i]);
    $size++;
}

var_dump($arr);

https://3v4l.org/mEHJU

It creates:

array(6) {
      [6]=>
      string(1) "f"
      [7]=>
      string(1) "e"
      [8]=>
      string(1) "d"
      [9]=>
      string(1) "c"
      [10]=>
      string(1) "b"
      [11]=>
      string(1) "a"
}

The keys are not from 0, but that can be corrected with array_values or does it need to be corrected without that function?

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

In your for loop, you have $i<=$size, shouldn't it be $i>=0? Secondly, you can reverse the array without any extra functions but since you are using $size variable, I hope you can use another variable $temp for swapping. Here is the sample code:

//original array
$arr = ['a','b','c','d','e','f'];
print_r($arr);
$size = sizeof($arr);

for($i=0; $i<$size; $i++)
{
     $temp=$arr[$i];
     $arr[$i]=$arr[$size-1];
     $arr[$size-1]=$temp;
     $size--;

}
//reversed array
print_r ($arr);
OWS
  • 138
  • 12
  • 1
    Added another answer with swapping method, using neither extra function nor any new variable. – OWS Sep 27 '17 at 10:04
0

Here is another solution using same variable names as yours and using swapping method, (also here you may assume $i as index from left and $size as index from right.

$arr = ['a','b','c','d','e','f'];
print_r($arr);
$i=0;

while ($i < sizeof($arr)/2){
    $size=$arr[$i];
    $arr[$i]=$arr[sizeof($arr)-1-$i];
    $arr[sizeof($arr)-1-$i]=$size;  
    $i=$i+1;
}

//reversed array
print_r ($arr);
OWS
  • 138
  • 12
0
      <?php
   $arr = ['a','b','c','d','e','f'];
   print_r($arr);
   $size = sizeof($arr);
  for($i=5; $i<=$size; $i--)
  {
    if($i==$size-1)
     {
     echo '['.'"'.$arr[$i].'"',',';
     }
    if($i<$size && $i!=5 && $i>0)
     {
    echo '"'.$arr[$i].'"'.','; 
     }
    if($i==0)
     {
    echo '"'.$arr[$i].'"'.']'; 
     }
 }
  ?>//hope this helps you....
-1

ok so this is my last try

$array = ['a','b','c','d','e','f'];
$count = count($array);
for($i = 0; $i < $count/2; $i++){
  $array[$i] ^= $array[$count-$i-1] ^= $array[$i] ^= $array[$count-$i-1];
}

it is base on Is there a PHP function for swapping the values of two variables?

and if you cant even use count() you can do simple foreach loop

$count = 0;
foreach($array as $one)
 $count++;
user3350597
  • 471
  • 1
  • 4
  • 14