简体   繁体   中英

Reverse the order of elements in an array

I have an array that looks like this:

$names = array('joey','kaiba','marik','yugi');

I want to reverse the order of the elements inside this array that would look something like this:

$names = array('yugi','marik','kaiba','joey');

Is there an existing PHP function that would allow me to do this?

There is a inbuilt function that php provide is array_reverse()

<?php

$names = array('joey','kaiba','marik','yugi');
print_r(array_reverse($names));

?>
$names = array('joey','kaiba','marik','yugi');
$names=array_reverse($names);

The PHP function array_reverse() does exactly what you're looking for:

$names = array('joey','kaiba','marik','yugi');
$reversed = array_reverse($names);

print_r($reversed);

Output:

Array (
    [0] => yugi
    [1] => marik
    [2] => kaiba
    [3] => joey
)

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