Reverse an array in PHP by using recursion

Recursion

F(0) = 0, F(1) = 1
F(N) = F(N-1) + F(N-2) (N > 1)
function F(int $n): int {
if ($n == 0) return 0; // Stop condition
if ($n == 1) return 1; // Stop condition
return F($n - 1) + F($n - 2);}echo 'F(18) = ' . F(18); // F(18) = 2584
if ($n == 0) return 0;
if ($n == 1) return 1;

Reversing an array

function reverseArray(array $array) {
$result = [];
foreach ($array as $key => $value) {
// check if it is an array
if (is_array($value)) {
// insert into the front of the result array
$result = insertToFront($key, reverseArray($value), $result);
continue;
}
$result = insertToFront($key, $value, $result);
}
return $result;
}
$result = insertToFront($key, $value, $result);
$result =  insertToFront($key, reverseArray($value), $result);
function insertToFront($key, $value, $array) {$result = [];
// insert into the front of the result array
$result[$key] = $value;
// append all the rest data
foreach ($array as $oldKey => $oldValue) {
$result[$oldKey] = $oldValue;
}
return $result;
}
// Indexed array
$inputArray = [1, 2, 3, 4, 5];
echo 'Before Reverse:' . PHP_EOL;
var_dump($inputArray);
echo 'After Reverse:' . PHP_EOL;
var_dump(reverseArray($inputArray));
Before Reverse:array (size=5)
0 => int 1
1 => int 2
2 => int 3
3 => int 4
4 => int 5
After Reverse:array (size=5)
4 => int 5
3 => int 4
2 => int 3
1 => int 2
0 => int 1
// Associative array
$inputArray = ['a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5];
echo 'Before Reverse:' . PHP_EOL;
var_dump($inputArray);
echo 'After Reverse:' . PHP_EOL;
var_dump(reverseArray($inputArray));
Before Reverse:array (size=5)
'a' => int 1
'b' => int 2
'c' => int 3
'd' => int 4
'e' => int 5
After Reverse:array (size=5)
'e' => int 5
'd' => int 4
'c' => int 3
'b' => int 2
'a' => int 1
array (size=5)
'e' => int 5
'd' => int 4
'c' => int 3
'b' => int 2
'a' => int 1
$inputArray = ['a' => [1, 2, 'a' => ['a', 'b' => ['a', 'b', 'c', 'd' => [1, 2, 3]], 'c'], 3], 2, 'c' => 'e', 4, 5];
// set configurable variables in php.ini at runtime
// for var_dump() showing all data
ini_set('xdebug.var_display_max_depth', -1);
ini_set('xdebug.var_display_max_children', -1);
ini_set('xdebug.var_display_max_data', -1);
echo 'Before Reverse:' . PHP_EOL;
var_dump($inputArray);
echo 'After Reverse:' . PHP_EOL;
var_dump(reverseArray($inputArray));
Before Reverse:array (size=5)
'a' =>
array (size=4)
0 => int 1
1 => int 2
'a' =>
array (size=3)
0 => string 'a' (length=1)
'b' =>
array (size=4)
0 => string 'a' (length=1)
1 => string 'b' (length=1)
2 => string 'c' (length=1)
'd' =>
array (size=3)
0 => int 1
1 => int 2
2 => int 3
1 => string 'c' (length=1)
2 => int 3
0 => int 2
'c' => string 'e' (length=1)
1 => int 4
2 => int 5
After Reverse:array (size=5)
2 => int 5
1 => int 4
'c' => string 'e' (length=1)
0 => int 2
'a' =>
array (size=4)
2 => int 3
'a' =>
array (size=3)
1 => string 'c' (length=1)
'b' =>
array (size=4)
'd' =>
array (size=3)
2 => int 3
1 => int 2
0 => int 1
2 => string 'c' (length=1)
1 => string 'b' (length=1)
0 => string 'a' (length=1)
0 => string 'a' (length=1)
1 => int 2
0 => int 1

Resources

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Yongyao Yan

Yongyao Yan

16 Followers

I am a programmer and a technical writer. To find more programming tutorials, please visit my website: https://www.codebilby.com