简体   繁体   中英

PHP reverse the order of sub array

Here is an array example I have.

Array
(
    [0] => Array
        (
            [0] => a
            [1] => b
            [2] => c
        )

)

I need the order of the sub array to be reversed. I know the function I need to use is "reverse_array" but I do not know how to apply it to an array within an array.

The function you need to use is array_reverse() , but I'm assuming that's the one you were talking about. To answer your question, you simply specify the array item instead of the main array:

$array[0] = array_reverse($array[0]);
//     ^                         ^

If you instead wish to reverse all sub-arrays in an array, you can use array_map() :

$array = array_map('array_reverse', $array);

use this

array_reverse($array[0]);

or use

$newSortedArray=array();
foreach ($array as $arr){
  $newSortedArray= array_reverse($arr);
}

echo "<pre>";
print_r($newSortedArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM