faqts : Computers : Programming : Languages : PHP : Function Libraries : Arrays

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

12 of 15 people (80%) answered Yes
Recently 7 of 7 people (100%) answered Yes

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;
}