본문 바로가기

'.' Programs/PHP

[PHP] 배열함수 array_unique() - 중복된 배열값 제거.

php 배열함수 array_unique 예제) 중복된 배열값을 제거한다.


문자나, 숫자의 배열값이 서로 중복으로 들어가 있을때에 그 중복값을 제거하고 표시하게 된다.

"123, 1234" 의 값이 있다면 1234 로 값이 나오며,
"사과, 배, 딸기, 사과, 배, 바나나, 포도" 의 값이 있다면 사과, 배, 딸기, 바나나, 포도 의 값이 나온다.

array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )

Takes an input array and returns a new array without duplicate values.

Note that keys are preserved. array_unique() sorts the values treated as string at first, then will keep the first key encountered for every value, and ignore all following keys. It does not mean that the key of the first related value from the unsorted array will be kept. 

NoteTwo elements are considered equal if and only if (string) $elem1 === (string) $elem2. In words: when the string representation is the same. The first element will be used. 

Example #1 array_unique() example

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

The above example will output:

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


Example #2 array_unique() and types

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

The above example will output:

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



I made an array_unique function to handle deep arrays:

<?php
    
function array_unique_deep($array) {
        
$values=array();
        
//ideally there would be some is_array() testing for $array here...
        
foreach ($array as $part) {
            if (
is_array($part)) $values=array_merge($values,array_unique_deep($part));
            else 
$values[]=$part;
        }
        return 
array_unique($values);
    }
    
    
$test=array(123,456,array(789,123,array(456),789),array(123));
    
print_r(array_unique_deep($test));
    
?>

outputs

Array
(
    [0] => 123
    [1] => 456
    [2] => 789
)
 



if you want to close the gaps into the keys after using array_unique() you can use array_values() afterwards. Example:

<?php
= array("one""two""two""three")
array_unique(a);
/* will lead to:
a[0] = "one"
a[1] = "two"
a[3] = "three"
*/
array_values(a);
/* Now we've got:
a[0] = "one"
a[1] = "two"
a[2] = "three"
*/
?> 



It's often faster to use a foreache and array_keys than array_unique: 

    <?php 

    $max 
1000000
    
$arr range(1,$max,3); 
    
$arr2 range(1,$max,2); 
    
$arr array_merge($arr,$arr2); 

    
$time = -microtime(true); 
    
$res1 array_unique($arr); 
    
$time += microtime(true); 
    echo 
"deduped to ".count($res1)." in ".$time
    
// deduped to 666667 in 32.300781965256 

    
$time = -microtime(true); 
    
$res2 = array(); 
    foreach(
$arr as $key=>$val) {    
        
$res2[$val] = true
    } 
    
$res2 array_keys($res2); 
    
$time += microtime(true); 
    echo 
"<br />deduped to ".count($res2)." in ".$time
    
// deduped to 666667 in 0.84372591972351 

    
?>  


This is how to merge 2 comma separated lists with unique value only.

<?php
  $list1 
"4444, 5555, 6666";
  
$list2 "4444, 5555, 7777";

  
// combine both lists with unique values only
  
$list3 implode("," array_unique(array_merge(explode(",",$list1),explode(","$list2))));

  echo 
$list3;
?>

The result is: 4444,5555,6666,7777
  



I'm not sure why you'd use any kind of loop to get the duplicates in an array. Here's a handy little function which does exactly that using some of PHP's other array_* functions.

<?php
function array_not_unique$a = array() )
{
  return 
array_diff_key$a array_unique$a ) );
}
?>

Some arrays for testing:

<?php
$person 
= array();
$person[1] = "person_1";
$person[2] = "person_2";
$person[3] = "person_3";
$person[4] = "person_4";
$person[5] = "person_5";
$person[6] = "person_2"# DUPE
$person[7] = "person_4"# DUPE
$person[8] = "person_4"# DUPE
echo '<pre>',print_r(array_not_unique($person),1),'</pre>';
$person = array();
$person[] = 1;
$person[] = 2;
$person[] = 3;
$person[] = 4;
$person[] = 5;
$person[] = 2# DUPE
$person[] = 4# DUPE
$person[] = 4# DUPE
echo '<pre>',print_r(array_not_unique($person),1),'</pre>';
?>

Output:
Array
(
    [6] => person_2
    [7] => person_4
    [8] => person_4
)

Array
(
    [5] => 2
    [6] => 4
    [7] => 4
)


출처 : http://hosting.websearch.kr/73

''.' Programs > PHP' 카테고리의 다른 글

php에서 원격mssql 접속  (0) 2012.06.01
[PHP] 배열 함수 정리.  (0) 2012.05.10
[PHP] 클립보드  (0) 2012.04.09
[PHP] print_r 과 var_dump 의 차이.  (0) 2012.04.04
[PHP] 막강 기능 배열..  (0) 2012.03.16