Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Need to recursively reverse an array

I need to recursively reverse a HUGE array that has many levels of sub arrays, and I need to preserve all of the keys (which some are int keys, and some are string keys), can someone please help me? Perhaps an example using array_reverse somehow? Also, is using array_reverse the only/best method of doing this?

Thanks :)

like image 443
SoLoGHoST Avatar asked Sep 04 '26 05:09

SoLoGHoST


1 Answers

Try this:

function array_reverse_recursive($arr) {
    foreach ($arr as $key => $val) {
        if (is_array($val))
            $arr[$key] = array_reverse_recursive($val);
    }
    return array_reverse($arr);
}
like image 164
Jacob Relkin Avatar answered Sep 06 '26 18:09

Jacob Relkin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!