Entry
How can I reverse a array in PHP 3.0?
Sep 3rd, 2000 14:53
Ben Udall, Jim Bruno Goldberg, http://www.php.net/manual/function.array-reverse.php
If you're using PHP4, there's a built-in function called
reverse_array()
$old_array = array('zero', 'one', 'two', three');
$new_array = reverse_array($old_array);
$new_array should now be equivalent to
array('three', 'two', 'one', 'zero')
If you don't have the reverse_array() function available. Below is a
function to do the same thing.
function reverse_array($in_array)
{
$size = count($in_array);
end($in_array)
for ($i=0; $i<$size; ++$i)
{
$key = key($in_array);
$out_array[$key] = $in_array[$key];
prev($in_array);
}
return $out_array;
}