简体   繁体   中英

How to reorder array in PHP?

I need to reorder an array in PHP.

The array:

Array
(
    [0] => /riado/?p=1
    [1] => /riado/?p=2
    [2] => /riado/?p=3
    [3] => /riado/?p=4
)

How to reorder to:

Array
(
    [0] => /riado/?p=4
    [1] => /riado/?p=3
    [2] => /riado/?p=2
    [3] => /riado/?p=1
)

I have searched but I can't find much clues. Can you give me some clues on how to achieve this?

$array = array_reverse($array);

Will reverse the contents of $array , no matter the sort order of the contents.

rsort($array);

Will sort the array in reverse alphabetical order.

rsort($arr);

这应该够了吧

A simple rsort should produce the results you require if you want to sort the array in reverse order. (If you simply want to swap the elements from first to last, etc. you could of course use array_reverse or simply iterate over the array from last to first.)

reverse sort example:

<?php
 $testArray = array('/riado/?p=1', '/riado/?p=2', '/riado/?p=3', '/riado/?p=4');
 rsort($testArray);
 print_r($testArray);
?>

array_reverse example:

<?php
 $testArray = array('/riado/?p=1', '/riado/?p=2', '/riado/?p=3', '/riado/?p=4');
 $testArray = array_reverse($testArray);
 print_r($testArray);
?>

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