Delete equal values within a dynamic array

3

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

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

$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 only for example, my array can have n values.

    
asked by anonymous 03.10.2017 / 13:31

1 answer

2

php already has a function that does everything you want:

array_unique($array);

It removes the duplicates and reorders the indices of the array.

  

See working at Ideone .

03.10.2017 / 13:35