How to remove all same values from an array in php?

Member

by denis , in category: PHP General , a year ago

How to remove all same values from an array in php?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by cortez.connelly , 6 months ago

@denis 

You can remove all duplicate values from an array in PHP by using the array_unique() function. Here is an example:

1
2
3
4
5
6
7
8
// Define an array with duplicate values
$array = array(1, 2, 2, 3, 4, 4, 5);

// Remove duplicate values from the array
$uniqueArray = array_unique($array);

// Print the unique array
print_r($uniqueArray);


Output:

1
2
3
4
5
6
7
8
Array
(
    [0] => 1
    [1] => 2
    [3] => 3
    [5] => 4
    [6] => 5
)


As you can see, the array_unique() function removed all the duplicate values from the original array.