php – Deleting equal values ​​within a dynamic array

Question:

I have an array with several dynamic names that can change at any given time, what is the most correct way to go through this array and create a condition to exclude them equal? (in this case two elements with the same name)

I had seen something similar here but it was for fixed elements, I wanted to create a condition in which I would unset (command to remove an element from the array) on the repeated element, an example of what I want more or less below:

$array = ('pepsi','Coca','fanta','pepsi');
// verificaria se tem elemento igual
if( "a operação que estou na duvida"){
//daria unset em um dos valores duplicados
    unset($array["3"]);
} 
// uso pra ignorar as chaves do array e alinhar 
$array = array_values( $array );

Note: The values ​​are for example, my array can have n values.

Answer:

The php already has a function that does everything you want:

array_unique($array);

It removes duplicates and reorders the array's indices.

See working on Ideone .

Scroll to Top