如题所述
array_unique
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
array_unique â 移é¤æ°ç»ä¸éå¤çå¼
说æ
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array_unique() æ¥å array ä½ä¸ºè¾å ¥å¹¶è¿å没æéå¤å¼çæ°æ°ç»ã
注æé®åä¿çä¸åãarray_unique() å å°å¼ä½ä¸ºå符串æåºï¼ç¶å对æ¯ä¸ªå¼åªä¿ç第ä¸ä¸ªéå°çé®åï¼æ¥ç忽ç¥ææåé¢çé®åãè¿å¹¶ä¸æå³çå¨æªæåºç array ä¸åä¸ä¸ªå¼ç第ä¸ä¸ªåºç°çé®åä¼è¢«ä¿çã
Note: å½ä¸ä» å½ (string) $elem1 === (string) $elem2 æ¶ä¸¤ä¸ªåå 被认为ç¸åãå°±æ¯è¯´ï¼å½å符串ç表达ä¸æ ·æ¶ã 第ä¸ä¸ªåå å°è¢«ä¿çã
åæ°
array
è¾å ¥çæ°ç»ã
sort_flags
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
Sorting type flags:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
è¿åå¼
Returns the filtered array.
æ´æ°æ¥å¿
çæ¬
说æ
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
èä¾
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"
}
åè§
array_count_values() - ç»è®¡æ°ç»ä¸ææçå¼åºç°ç次æ°
注é
Note: Note that array_unique() is not intended to work on multi dimensional arrays.
(PHP 4 >= 4.0.1, PHP 5, PHP 7)
array_unique â 移é¤æ°ç»ä¸éå¤çå¼
说æ
array array_unique ( array $array [, int $sort_flags = SORT_STRING ] )
array_unique() æ¥å array ä½ä¸ºè¾å ¥å¹¶è¿å没æéå¤å¼çæ°æ°ç»ã
注æé®åä¿çä¸åãarray_unique() å å°å¼ä½ä¸ºå符串æåºï¼ç¶å对æ¯ä¸ªå¼åªä¿ç第ä¸ä¸ªéå°çé®åï¼æ¥ç忽ç¥ææåé¢çé®åãè¿å¹¶ä¸æå³çå¨æªæåºç array ä¸åä¸ä¸ªå¼ç第ä¸ä¸ªåºç°çé®åä¼è¢«ä¿çã
Note: å½ä¸ä» å½ (string) $elem1 === (string) $elem2 æ¶ä¸¤ä¸ªåå 被认为ç¸åãå°±æ¯è¯´ï¼å½å符串ç表达ä¸æ ·æ¶ã 第ä¸ä¸ªåå å°è¢«ä¿çã
åæ°
array
è¾å ¥çæ°ç»ã
sort_flags
The optional second parameter sort_flags may be used to modify the sorting behavior using these values:
Sorting type flags:
SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale.
è¿åå¼
Returns the filtered array.
æ´æ°æ¥å¿
çæ¬
说æ
5.2.10 Changed the default value of sort_flags back to SORT_STRING.
5.2.9 Added the optional sort_flags defaulting to SORT_REGULAR. Prior to 5.2.9, this function used to sort the array with SORT_STRING internally.
èä¾
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"
}
åè§
array_count_values() - ç»è®¡æ°ç»ä¸ææçå¼åºç°ç次æ°
注é
Note: Note that array_unique() is not intended to work on multi dimensional arrays.
温馨提示:答案为网友推荐,仅供参考