downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | conferences | my php.net

search for in the

array_search> <array_replace
[edit] Last updated: Wed, 19 Jun 2013

view this page in

array_reverse

(PHP 4, PHP 5)

array_reverseDevuelve un array con los elementos en orden inverso

Descripción

array array_reverse ( array $array [, bool $preserve_keys = false ] )

Toma un valor array y devuelve un nuevo array con el orden de los elementos invertido.

Parámetros

array

El array de entrada.

preserve_keys

Si se establece en TRUE las claves numéricas se conservarán. Las claves numéricas no se ven afectadas por este ajuste y se conservarán siempre.

Valores devueltos

Devuelve el array en orden inverso.

Historial de cambios

Versión Descripción
4.0.3 El parámetro preserve_keys fue añadido.

Ejemplos

Ejemplo #1 Ejemplo de array_reverse()

<?php
$input  
= array("php"4.0, array("verde""rojo"));
$reversed array_reverse($input);
$preserved array_reverse($inputtrue);

print_r($input);
print_r($reversed);
print_r($preserved);
?>

El resultado del ejemplo sería:

Array
(
    [0] => php
    [1] => 4
    [2] => Array
        (
            [0] => verde
            [1] => rojo
        )

)
Array
(
    [0] => Array
        (
            [0] => verde
            [1] => rojo
        )

    [1] => 4
    [2] => php
)
Array
(
    [2] => Array
        (
            [0] => verde
            [1] => rojo
        )

    [1] => 4
    [0] => php
)

Ver también

  • array_flip() - Intercambia todas las keys con sus valores asociados en un array



array_search> <array_replace
[edit] Last updated: Wed, 19 Jun 2013
 
add a note add a note User Contributed Notes array_reverse - [6 notes]
up
5
alfbennett at gmail dot com
3 years ago
Needed to just reverse keys. Don't flog me if there is a better way. This was a simple solution.

<?php
function array_reverse_keys($ar){
    return
array_reverse(array_reverse($ar,true),false);
}
?>
up
0
atulmvAThotmailDOTcoDOTuk
9 months ago
Here are a couple of routines to swap the order of two elements in an array

<?php

function array_swap_forward($arr,$elem)
{
$ndx = array_search($elem,$arr) - 1;
$b4 = array_slice($arr,0,$ndx);
$mid = array_reverse(array_slice($arr,$ndx,2));
$after = array_slice($arr,$ndx + 2);

return
array_merge($b4,$mid,$after);
}

function
array_swap_back($arr,$elem)
{
$ndx = array_search($elem,$arr);
$b4 = array_slice($arr,0,$ndx);
$mid = array_reverse(array_slice($arr,$ndx,2));
$after = array_slice($arr,$ndx + 2);

return
array_merge($b4,$mid,$after);
}

$arr =array('a','b','c','d','e','f');
print_r(array_swap_forward($arr,'c'));
echo
'<br>';
print_r(array_swap_back($arr,'c'));
?>
up
0
rahulavhad at yahoo dot com
12 years ago
This code can help in recursive reversing of the array...

<?php
$arr1
= array(2,1,array(5,2,1,array(9,8,7)),5,0);
$arr1 = array_reverse($arr1);

function
Reverse_Array($array)
{   
$index = 0;
    foreach (
$array as $subarray)
    {    if (
is_array($subarray))
        {   
$subarray = array_reverse($subarray);
           
$arr = Reverse_Array($subarray);
           
$array[$index] = $arr;
        }
        else {
$array[$index] = $subarray;}
       
$index++;
    }
    return
$array;
}

$arr2 = Reverse_Array($arr1);
?>
up
-1
david at audiogalaxy dot com
13 years ago
As a further clarification: key-value pairs have an order within an array completely separate from whatever the keys happen to be - the order in which you add them.  This is the order that functions like each() and next() will move their pointer through the array.
If you add to an array without specifying the key, like $array[] = value; then an internal counter supplies the key value and then the numerical order of your keys will be identical to the the internal order.  If you "leave holes" - jumping ahead by specifying a higher number for the key, like $array[1000] = value; the internal counter gets pushed forward appropriately.  Other than its effect on this internal counter, specifying a numerical key seems no different than specifying a string.
However, some array functions, like array_merge() and array_reverse() treat keys that are numbers differently from keys that are not.
up
-1
david at audiogalaxy dot com
13 years ago
With associative arrays array_reverse() keeps key => value pairs matched but reverses the order of the array as spaned by functions like each().  With numerical indexes array_reverse not only reverses position (as spaned by each) but also renumbers the keys.
Both cases seem to be what people would generally want: indeed without the renumbering behavior, someone refering to array elements by numerical key wouldn't think array_reverse did anything.
However, people who are trying to keep numerical keys associated with their values - e.g. trying to have holes in their arrays - will be foiled by the renumbering.  The most telling results come from applying array_reverse() to arrays with mixed keys (some numbers and some strings).  The strings stay attached and the rest of the keys get renumbered around them - most annoying if you are thinking what you've got is an associative array but some of your keys happen to be numbers.
up
-5
virtual89 at gmail dot com
10 months ago
With array_reverse() if the keys are strings, they remain the same, example:

<?php
$a
= array ("zero"=>"hello","one"=>"world");
var_dump (array_reverse($a));
?>

Outputs:

array(2) {
["one"]=>
string(5) "world"
["zero"]=>
string(5) "hello"
}

But if the keys are numbers, they will be changed starting from 0, example:

<?php
$a
= array (10=>"hello",20=>"world");
var_dump (array_reverse($a));
?>

Outputs:

array(2) {
[0]=>
string(5) "world"
[1]=>
string(5) "hello"
}

 
show source | credits | sitemap | contact | advertising | mirror sites