超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<array_reducearray_search>
Last updated: Fri, 22 Jun 2007

array_reverse

(PHP 4 , PHP 5)

array_reverse --  返回一个单元顺序相反的数组

说明

array array_reverse ( array array [, bool preserve_keys])

array_reverse() 接受数组 array 作为输入并返回一个单元为相反顺序的新数组,如果 preserve_keysTRUE 则保留原来的键名。

例子 1. array_reverse() 例子

<?php
$input 
= array ("php", 4.0, array ("green", "red"));
$result = array_reverse ($input);
$result_keyed = array_reverse ($input, TRUE);
?>

这将使 $result$result_keyed 具有相同的单元,但是注意键名的区别。$result$result_keyed 的打印输出显示分别为:

Array
(
   [0] => Array
       (
           [0] => green
           [1] => red
       )

   [1] => 4
   [2] => php
)
Array
(
   [2] => Array
       (
           [0] => green
           [1] => red
       )

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

注: 第二个参数是 PHP 4.0.3 中新加的。

参见 array_flip()




add a note add a note User Contributed Notes
array_reverse
david at audiogalaxy dot com
04-Mar-2000 04:40
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.

<array_reducearray_search>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22