Archive for 十一月 2009

 
 

数组处理中的array_unique()与array_merge()

array_unique()定义和用法

array_unique() 函数移除数组中的重复的值,并返回结果数组。 当几个数组元素的值相等时,只保留第一个元素,其他的元素被删除。 返回的数组中键名不变。

提示和注释

注释:被返回的数组将保持第一个数组元素的键类型。

例子

<?php
$a=array("a"=>"Cat","b"=>"Dog","c"=>"Cat");
print_r(array_unique($a));
?>

输出:

Array ( [a] => Cat [b] => Dog )

array_merge()定义和用法

array_merge()数组的作用是将一个或多个数组合并为一个数组。

Example 1 案例1

<?php
$a1=array("a"=>"Horse","b"=>"Dog");
$a2=array("c"=>"Cow","b"=>"Cat");
print_r(array_merge($a1,$a2));
?>

The output of the code above will be: 上述代码将输出下面的结果:

Array ( [a] => Horse [b] => Cat [c] => Cow )

Example 2 案例2

Using only one array parameter. 仅使用一个数组参数:

<?php
$a=array(3=>"Horse",4=>"Dog");
print_r(array_merge($a));
?>

The output of the code above will be: 上述代码将输出下面的结果:

Array ( [0] => Horse [1] => Dog )