how can i reverse an order of a array without using a built in function like array_reverse() and rsort() .

    how can i reverse an order of a array without using a built in function like array_reverse() and rsort() .

    Why do you not want to use an existing function?

    Anyway, one simple way is to loop from the first element to the middle, swapping each element at position i with the element at position i from the back (i.e., swap the first with the last element, then the second with the second last element, until the middle of the array).

      Any chance of an example only i did try it and couldnt get it to work:

      $thearray = array(1=>"first", 2=>"second", 3=>"third", 4=>"forth");

        You can try:

        <?php
        function swap(&$a, &$b) {
            $t = $a;
            $a = $b;
            $b = $t;
        }
        
        $array = array("first", "second", "third", "fourth");
        
        $mid = (int)(count($array) / 2);
        for ($i = 0; $i < $mid; ++$i) {
            swap($array[$i], $array[count($array)-$i-1]);
        }
        
        print_r($array);
        ?>
          Write a Reply...