array_unique
(PHP 4 >= 4.0.1, PHP 5)
array_unique — 배열에서 중복된 값을 제거한다
설명
array array_unique
( array $array
)
array_unique()는 입력 array 를 취해서 중복값이 없는 새로운 배열을 반환한다.
키는 보존된다는 것에 유의한다. array_unique()는 우선 문자열로 취급되는 값들을 정렬하고, 모든 값중 첫번째로 만나는 키를 보유하게 될것이고, 다음의 모든 키들은 무시될것이다. 이 말의 의미는 정렬되지 않은 array 의 첫번째 관련 값의 키가 유지된다는 것이 아니다.
Note: 두 구성요소가 동치라고 볼수 있는 때는 (string) $elem1 === (string) $elem2 일때 만이다. In words: when the string representation is the same. 첫번째 원소가 사용될것이다.
Example#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 )
Example#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" }