How to reverse an array in php WITHOUT using the array reverse method

0 votes

I have an array called reverse array and I want to reverse it.

This is my code:

<?php

//Task 21 reverse array

$reverseArray = array(1, 2, 3, 4);
$tmpArray = array();
$arraySize = sizeof($reverseArray);

for($i<arraySize; $i=0; $i--){
    echo $reverseArray($i);
}

?>

I want to know how to reverse an array without using the array_reverse method. Can someone please point out the mistake in my code?

Jun 12, 2022 in PHP by narikkadan
• 63,600 points
2,390 views
<?php

//Task 21 reverse array

$reverseArray = array(1, 2, 3, 4);

$tmpArray = [];

$arraySize = sizeof($reverseArray)-1;

for($i=$arraySize; $i>=0; $i--)

{

    $tmpArray[]=$reverseArray[$i];

}

print_r($tmpArray);

?>

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
0 votes

Try this:

<?php
  $array = array(1, 2, 3, 4);
  $size = sizeof($array);

  for($i=$size-1; $i>=0; $i--){
      echo $array[$i];
  }
?>

It worked for me. I hope this helps you.

answered Jun 13, 2022 by Kithuzzz
• 38,000 points

edited Mar 5
0 votes
<?php

//Task 21 reverse array

$reverseArray = array(1, 2, 3, 4);

$tmpArray = [];

$arraySize = sizeof($reverseArray)-1;

for($i=$arraySize; $i>=0; $i--)

{

    $tmpArray[]=$reverseArray[$i];

}

print_r($tmpArray);

?>
answered Jan 11, 2024 by Nandhini

edited Mar 5
0 votes

Corrected Code :
<?php
$reverseArray = array(1, 2, 3, 4);
$arraySize = sizeof($reverseArray);

for ($i = $arraySize - 1; $i >= 0; $i--) {
    echo $reverseArray[$i];
    echo " "; // Add a space for better readability
}
?>

answered Feb 15 by Abi

edited Mar 6
0 votes
hhhhjghjghjghghghjghghg
answered Feb 16 by anonymous

edited Mar 6

Related Questions In PHP

0 votes
1 answer

How to remove duplicate values from an array in PHP?

Hello @kartik, Use array_unique(): Example: $array = array(1, 2, 2, 3); $array ...READ MORE

answered Sep 15, 2020 in PHP by Niroj
• 82,840 points
2,836 views
0 votes
1 answer

How to delete element by value in array (not key) using php?

Hello @kartik, Using array_search() and unset, try the following: if (($key = ...READ MORE

answered Sep 16, 2020 in PHP by Niroj
• 82,840 points
2,201 views
0 votes
1 answer

How to get the list of specific files in a directory using php?

Hello @kartik, You'll be wanting to use glob() Example: $files = ...READ MORE

answered Nov 6, 2020 in PHP by Niroj
• 82,840 points
2,389 views
0 votes
1 answer

How to pass an array via $_GET in php?

Hii, You can pass an associative array to http_build_query() and ...READ MORE

answered Nov 22, 2020 in PHP by Niroj
• 82,840 points
4,778 views
0 votes
0 answers

How to convert an array to a string in PHP?

What can I do to get the ...READ MORE

May 30, 2022 in PHP by Kichu
• 19,040 points
693 views
0 votes
0 answers

How to convert a string to an array in php

I want to convert a string into ...READ MORE

Jun 1, 2022 in PHP by Kichu
• 19,040 points
703 views
0 votes
1 answer

How to check if array is multidimensional or not?

Since the 'second dimension' could be just ...READ MORE

answered Nov 5, 2018 in Others by DataKing99
• 8,250 points
6,254 views
0 votes
1 answer

How to store input value into array then localstorage?

Hello @ abhittac, You have create the array everytime the ...READ MORE

answered Jul 24, 2020 in Java-Script by Niroj
• 82,840 points
9,332 views
0 votes
1 answer

Convert a PHP object to an associative array

Start with simply typecasting the line:- $array = ...READ MORE

answered Feb 23, 2022 in PHP by Aditya
• 7,680 points
2,251 views
0 votes
0 answers

php var_dump() vs print_r()

What is the difference between var_dump() and print_r() in terms of ...READ MORE

May 28, 2022 in PHP by Kichu
• 19,040 points
597 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP