Using PHP's array_unique() Function to Remove Duplicate Array Elements

This article explains PHP's array_unique() function, detailing its syntax, parameters, and return value, and provides clear examples showing how to remove duplicate elements from arrays and optionally sort them using the $sort_flag parameter.

php Courses
php Courses
php Courses
Using PHP's array_unique() Function to Remove Duplicate Array Elements

In PHP programming, the array_unique() function is commonly used to remove duplicate values from an array and return a new array containing only unique elements.

The function signature is:

array_unique(array $array, int $sort_flag = SORT_STRING): array

Parameters: $array: the input array that may contain duplicate values. $sort_flag (optional): determines how the array elements are sorted during the uniqueness check. The default is SORT_STRING, which treats elements as strings and sorts them lexicographically. Another option is SORT_REGULAR, which uses regular comparison.

The function returns a new array with duplicate entries removed, preserving the first occurrence of each value.

Example 1 demonstrates basic usage:

<?php<br/>// Define an array with duplicate elements<br/>$fruits = array("apple", "banana", "orange", "apple", "melon", "banana");<br/><br/>// Remove duplicates<br/>$uniqueFruits = array_unique($fruits);<br/><br/>// Output the result<br/>print_r($uniqueFruits);<br?>

Running this code produces:

Array<br/>(<br/>    [0] => apple<br/>    [1] => banana<br/>    [2] => orange<br/>    [4] => melon<br/>)

As shown, the duplicate "apple" and "banana" entries are removed, while the other elements remain unchanged.

Example 2 shows how to use the optional $sort_flag parameter to sort the unique values as strings:

<?php<br/>$numbers = array(1, 3, 5, 2, 5, 4);<br/><br/>// Remove duplicates and sort as strings<br/>$uniqueNumbers = array_unique($numbers, SORT_STRING);<br/><br/>// Output the result<br/>print_r($uniqueNumbers);<br?>

The output is:

Array<br/>(<br/>    [0] => 1<br/>    [1] => 2<br/>    [2] => 3<br/>    [4] => 4<br/>    [5] => 5<br/>)

By specifying SORT_STRING, the numeric values are treated as strings and sorted in ascending order after duplicates are removed.

In summary, array_unique() is a convenient PHP function for deduplicating arrays, and the optional $sort_flag allows developers to control the sorting behavior of the resulting unique array, enhancing code efficiency in backend development.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

BackendPHPSortingduplicate removalarray_unique
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.