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

array_unique

(PHP 4 >= 4.0.1, PHP 5)

array_unique -- 移除数组中重复的值

说明

array array_unique ( array array)

array_unique() 接受 array 作为输入并返回没有重复值的新数组。

注意键名保留不变。array_unique() 先将值作为字符串排序,然后对每个值只保留第一个遇到的键名,接着忽略所有后面的键名。这并不意味着在未排序的 array 中同一个值的第一个出现的键名会被保留。

注: 当且仅当 (string) $elem1 === (string) $elem2 时两个单元被认为相同。就是说,当字符串的表达一样时。

第一个单元将被保留。

例子 1. array_unique() 例子

<?php
$input
= array ("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique ($input);
print_r($result);
?>

上例将输出:

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

例子 2. array_unique() 和类型

<?php
$input
= array (4,"4","3",4,3,"3");
$result = array_unique ($input);
var_dump($result);
?>

本脚本将输出:

array(2) {
  [0] => int(4)
  [2] => string(1) "3"
}




add a note add a note User Contributed Notes
array_unique
moose at ucdavis dot edu
16-Aug-2000 05:08
Here's a little function I wrote that's similar to array_unique, for PHP3 users.  It actually removes duplicated elements, but only works for 1 dimensional arrays.  It also doesn't return a value, it changes the input array:

<?php
function array_unique(&$thearray)
{sort($thearray);
reset($thearray);

$newarray = array();
$i = 0;

$element = current($thearray);
for ($n=0;$n<sizeof($thearray);$n++)
{if (next($thearray) != $element)
 {$newarray[$i] = $element;
  $element = current($thearray);
  $i++;
 }
}
$thearray = $newarray;
}
?>

<array_uintersectarray_unshift>
 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