• 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
  • VBA
  • 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.  Delete Cookie Using php
  • 2.  php in_array – Check If Value is in php Array
  • 3.  php preg_match – Find Pattern in String Using Regex (Regular Expression)
  • 4.  Creating Custom category.php With Pagination
  • 5.  php str_split() – Convert String into Array of Equal Length Substrings
  • 6.  How to Check If String Contains Substring in PHP
  • 7.  php property_exists() – Check if Property Exists in Class or Object
  • 8.  php Delete Files with php Unlink Function
  • 9.  preg_replace php – Use Regex to Search and Replace
  • 10.  php sin – Find Sine of Number in Radians Using php sin() Function

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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy