Saggi Posted December 22, 2019 Share Posted December 22, 2019 Hi all, Need help in solving this task using PHP built in function : array_reverse() and without it. Appreciate it in explaining the code as I'm completely a newbie. function reverseArray($Arr) { } $fruits = ["Mango","Strawberry","Bananas","Pineapple"]; $result = reverseArray($fruits); Many thanks, Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/ Share on other sites More sharing options...
Barand Posted December 22, 2019 Share Posted December 22, 2019 (edited) HINT: try using array_pop() and build a new array or, method 2, sort the array keys in reverse order Edited December 22, 2019 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572781 Share on other sites More sharing options...
NotSunfighter Posted December 22, 2019 Share Posted December 22, 2019 Or use array_reverse' <?php $input = array("Name", 1, 2, 3, array("red", "green"), 4, "life"); $reversed = array_reverse($input); print_r($input); echo '---<br>'; print_r($reversed); echo '---<br>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572786 Share on other sites More sharing options...
Barand Posted December 22, 2019 Share Posted December 22, 2019 Very good! But the question explicitly wanted to do it without array_reverse 4 hours ago, Saggi said: Need help in solving this task using PHP built in function : array_reverse() and without it. Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572788 Share on other sites More sharing options...
Saggi Posted December 22, 2019 Author Share Posted December 22, 2019 4 hours ago, NotSunfighter said: Or use array_reverse' <?php $input = array("Name", 1, 2, 3, array("red", "green"), 4, "life"); $reversed = array_reverse($input); print_r($input); echo '---<br>'; print_r($reversed); echo '---<br>'; ?> $fruits = ["Mango","Strawberry","Bananas","Pineapple"]; function reverseArray($Arr) { return array_reverse($Arr); } $result = reverseArray($fruits); print_r($result); Output: Array ( [0] => Pineapple [1] => Bananas [2] => Strawberry [3] => Mango ) Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572800 Share on other sites More sharing options...
Barand Posted December 22, 2019 Share Posted December 22, 2019 So you can do it the easy way. What about the solutions not using array_reverse? Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572801 Share on other sites More sharing options...
Saggi Posted December 22, 2019 Author Share Posted December 22, 2019 (edited) 15 minutes ago, Barand said: So you can do it the easy way. What about the solutions not using array_reverse? I tried without using array_reverse and Here is my other solution: $fruits = ["Mango","Strawberry","Bananas","Pineapple"]; function reverseArray($Arr) { $fruitsReverse = []; for($i=count($Arr)-1; $i>=0; $i--) { array_push($fruitsReverse,$Arr[$i]); } return $fruitsReverse; } $result = reverseArray($fruits); print_r($result); Edited December 22, 2019 by Saggi Quote Link to comment https://forums.phpfreaks.com/topic/309734-reverse-an-array/#findComment-1572804 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.