array_reverse
(PHP 4, PHP 5)
array_reverse — Return an array with elements in reverse order
Description
array array_reverse
( array
$array
[, bool $preserve_keys
= false
] )
Takes an input array
and returns a new array with
the order of the elements reversed.
Parameters
-
array
-
The input array.
-
preserve_keys
-
If set to
TRUE
numeric keys are preserved. Non-numeric keys are not affected by this setting and will always be preserved.
Return Values
Returns the reversed array.
Changelog
Version | Description |
---|---|
4.0.3 |
The preserve_keys parameter was added.
|
Examples
Example #1 array_reverse() example
<?php
$input = array("php", 4.0, array("green", "red"));
$reversed = array_reverse($input);
$preserved = array_reverse($input, true);
print_r($input);
print_r($reversed);
print_r($preserved);
?>
The above example will output:
Array ( [0] => php [1] => 4 [2] => Array ( [0] => green [1] => red ) ) Array ( [0] => Array ( [0] => green [1] => red ) [1] => 4 [2] => php ) Array ( [2] => Array ( [0] => green [1] => red ) [1] => 4 [0] => php )
- PHP Руководство
- Функции по категориям
- Индекс функций
- Справочник функций
- Расширения, относящиеся к переменным и типам
- Массивы
- array_change_key_case
- array_chunk
- array_column
- array_combine
- array_count_values
- array_diff_assoc
- array_diff_key
- array_diff_uassoc
- array_diff_ukey
- array_diff
- array_fill_keys
- array_fill
- array_filter
- array_flip
- array_intersect_assoc
- array_intersect_key
- array_intersect_uassoc
- array_intersect_ukey
- array_intersect
- array_key_exists
- array_keys
- array_map
- array_merge_recursive
- array_merge
- array_multisort
- array_pad
- array_pop
- array_product
- array_push
- array_rand
- array_reduce
- array_replace_recursive
- array_replace
- array_reverse
- array_search
- array_shift
- array_slice
- array_splice
- array_sum
- array_udiff_assoc
- array_udiff_uassoc
- array_udiff
- array_uintersect_assoc
- array_uintersect_uassoc
- array_uintersect
- array_unique
- array_unshift
- array_values
- array_walk_recursive
- array_walk
- array
- arsort
- asort
- compact
- count
- current
- each
- end
- extract
- in_array
- key_exists
- key
- krsort
- ksort
- list
- natcasesort
- natsort
- next
- pos
- prev
- range
- reset
- rsort
- shuffle
- sizeof
- sort
- uasort
- uksort
- usort
Коментарии
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);
?>
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);
}
?>
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'));
?>