• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / PHP / How to Reverse Array in php Without a Function or Other Variables

How to Reverse Array in php Without a Function or Other Variables

March 24, 2022 Leave a Comment

To reverse an array in php without a function, we can use a loop and swap items in order.

$example = array("lion", "bear", "snake", "horse");

foreach(range(0,count($example)/2) as $i) {
    [$example[$i], $example[count($example) - $i - 1]] = [$example[count($example) - $i - 1],$example[$i]];
}

print_r($example);

//Output:
Array
(
    [0] => horse
    [1] => snake
    [2] => bear
    [3] => lion
)

When working with arrays and collections of data in php, it is useful to be able to easily change and manipulate our data structures easily.

One such operation is to be able to reverse an array.

We can use the php array_reverse() function, but sometimes you want to be able to perform operations without the use of built-in functions.

We can use the square bracket syntax for array destructuring assignment in a loop to swap items in a php array and reverse the array.

Implemented in php version 7.1, we can unpack arrays and use them to assign to other arrays and variables.

With this in mind, we can use a loop to reverse an array without the help of another function or without other variables easily in php.

To use a for loop for reversing a list without the array_reverse() function, we will swap items in the list in the following way.

First, we swap the first and last items. Next, we continue with swapping the second and second to last items, then the third and third to last items, until we reach the middle of the list.

$example = array("lion", "bear", "snake", "horse");

foreach(range(0,count($example)/2) as $i) {
    [$example[$i], $example[count($example) - $i - 1]] = [$example[count($example) - $i - 1],$example[$i]];
}

print_r($example);

//Output:
Array
(
    [0] => horse
    [1] => snake
    [2] => bear
    [3] => lion
)

Hopefully this article has been useful for you to learn how to reverse an array in php without using another function or variable.

Other Articles You'll Also Like:

  • 1.  php is_numeric – Check if Variable is a Number
  • 2.  Remove Specific Character from String Variable in php
  • 3.  Remove HTML Tags from String in php
  • 4.  php Convert Degrees to Radians with php deg2rad() Function
  • 5.  php asin – Find Arcsine and Inverse Sine of Number Using asin() Function
  • 6.  Delete Cookie Using php
  • 7.  How to Check If String Contains Substring in PHP
  • 8.  php acos – Find Arccosine and Inverse Cosine of Number
  • 9.  Remove Duplicates from Array in php with array_unique()
  • 10.  php Print Array – How to Print All Elements in Array

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy