Reverse an Array in PHP
Q. Write a PHP program to reverse an array
<?php
// Define an array
$originalArray = array(10, 20, 30, 40, 50);
// Manual reversal using loop
$reversedArray = array();
$length = count($originalArray);
for ($i = $length - 1; $i >= 0; $i--) {
$reversedArray[] = $originalArray[$i];
}
// Display original array
echo "Original Array: ";
foreach ($originalArray as $value) {
echo "$value ";
}
echo "<br>Reversed Array: ";
foreach ($reversedArray as $value) {
echo "$value ";
}
?>
Q. Write an algorithm to reverse an array in PHP
1. Start
2. Define an array originalArray[]
3. Get the length of the array
4. Initialize an empty array reversedArray[]
5. Loop from i = length - 1 to 0:
a. Append originalArray[i] to reversedArray[]
6. Print reversedArray[]
7.End
Quickly Find What You Are Looking For
OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.
point.com