array_unique移除数组中重复的值
array_unique接受$array,$sort_flags两个参数
1. $array
- 必须
- 要被处理的数组
2. $sort_flags
- 可选
- 指定排序比较方式
- 可选值 SORT_STRING,SORT_NUMERIC,SORT_REGULAR,SORT_LCALE_STRING
- 默认值SORT_STRING
/**
* Removes duplicate values from an array
* @link https://php.net/manual/en/function.array-unique.php
* @param array $array <p>
* The input array.
* </p>
* @param int $sort_flags [optional] <p>
* The optional second parameter sort_flags
* may be used to modify the sorting behavior using these values:
* </p>
* <p>
* Sorting type flags:
* </p><ul>
* <li>
* <b>SORT_REGULAR</b> - compare items normally
* (don't change types)
* </li>
* <li>
* <b>SORT_NUMERIC</b> - compare items numerically
* </li>
* <li>
* <b>SORT_STRING</b> - compare items as strings
* </li>
* <li>
* <b>SORT_LOCALE_STRING</b> - compare items as strings,
* based on the current locale
* </li>
* </ul>
* @return array the filtered array.
* @meta
*/
function array_unique(array $array, $sort_flags = SORT_STRING) { }
示例:
$array = [
'a' => 4,
'b' => "4",
'c' => "a",
'd' => 4,
'e' => 3,
'f' => "3"
];
$result = array_unique($array, SORT_STRING);
//$result = array_unique($array, SORT_NUMERIC);
//$result = array_unique($array, SORT_REGULAR);
//$result = array_unique($array, SORT_LOCALE_STRING);
var_dump($result);
//结果
//array(3) {
// 'a' =>
// int(4)
// 'c' =>
// string(1) "a"
// 'e' =>
// int(3)
//}