3v4l.org

run code in 300+ PHP versions simultaneously
<?php $array_words = ['harmony', 'Acrobat', 'harmony', 'harmony']; $array_words_unique = array_unique($array_words); //get a list of unique words from the original array $array_str = implode(",", $array_words); foreach ($array_words_unique as $word) { //count how many times the word occurs $count = substr_count($array_str, $word); //if it occurs more than once, remove the first occurence if ($count > 1) { //find the first position of the word in the string, then replace that with nothing $pos = strpos($array_str, $word); $array_str = substr_replace($array_str, "", $pos, strlen($word)); } } //convert back to an array, and filter any blank entries caused by commas with nothing between them $array_final = array_filter(explode(",", $array_str)); var_dump($array_final);
Output for 8.0.1 - 8.0.30, 8.1.0 - 8.1.33, 8.2.0 - 8.2.29, 8.3.0 - 8.3.27, 8.4.1 - 8.4.14
array(3) { [1]=> string(7) "Acrobat" [2]=> string(7) "harmony" [3]=> string(7) "harmony" }

preferences:
101.21 ms | 406 KiB | 5 Q