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 , 9 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.

Related Threads:

How to remove an array of items from an array in php?
How to store the string values into an array in php?
How to insert php array values into chart.js line chart?
How to remove elements from an array in PHP?
How to remove nan from array in php?
How to remove elements from php array?