Home >Backend Development >PHP Tutorial >PHP function introduction: array_unique() function
Introduction to PHP functions: array_unique() function, specific code examples are required
In PHP programming, we often need to operate and process arrays. One of the commonly used functions is the array_unique() function, which allows us to remove duplicate elements from an array and return a new array.
The syntax of the array_unique() function is as follows:
array_unique(array $array, int $sort_flag = SORT_STRING): array
Parameter explanation:
Let’s look at a specific example to learn more about how to use the array_unique() function.
//Define an array containing repeated elements
$fruits = array("apple", "banana", "orange", "apple", "melon", "banana");
// Use the array_unique() function to remove duplicate elements
$uniqueFruits = array_unique($fruits);
//Print out the array after deduplication
print_r($uniqueFruits);
?>
In the above code snippet, we define an array $fruits containing repeated elements. Then, we use the array_unique() function to deduplicate the array $fruits, and assign the deduplicated array to the $uniqueFruits variable. Finally, we print out the deduplicated array through the print_r() function.
Run the above code, the output result is as follows:
Array
(
[0] => apple [1] => banana [2] => orange [4] => melon
)
You can see that in the deduplicated array, the duplicate Only one of the elements "apple" and "banana" is retained, while the other elements remain unchanged.
When using the array_unique() function, there is an optional parameter $sort_flag, which is used to specify the way to sort the array elements. It has the following two values:
The following example demonstrates how to use the $sort_flag parameter:
$numbers = array(1, 3, 5, 2, 5, 4) ;
// Use the array_unique() function to remove duplicate elements and sort them in dictionary order
$uniqueNumbers = array_unique($numbers, SORT_STRING);
// Print output after deduplication Array
print_r($uniqueNumbers);
?>
Run the above code, the output result is as follows:
Array
(
[0] => 1 [1] => 2 [2] => 3 [4] => 4 [5] => 5
)
You can see that by specifying $sort_flag as SORT_STRING, the array elements are treated as strings and sorted in dictionary order. The final output deduplicated array is arranged in ascending order.
Summary:
The array_unique() function is a very convenient function in PHP, which allows us to quickly remove duplicate elements from an array. By specifying the $sort_flag parameter, we can also deduplicate array elements according to different sorting methods. In actual development, using the array_unique() function can greatly simplify the processing and operation of arrays and improve the efficiency of the code.
I hope the above introduction and sample code about the array_unique() function can help you. By learning and using this function, you can better process arrays.
The above is the detailed content of PHP function introduction: array_unique() function. For more information, please follow other related articles on the PHP Chinese website!