I’ve researched all sorts of ways, but I haven’t found a solution for this case.
Basically I have to see if the word repeats and just remove the first occurrence of it in the array. For example:
$array_words = ['harmony', 'Acrobat', 'harmony', 'harmony'];
How do I check the repeated word, just once, leaving the array like this:
$array_final = ['Acrobat', 'harmony', 'harmony'];
3
Answers
I threw together this simple loop, and explained it with comments
$output
valueGrumpyCrouton’s solution is probably neater, but here’s another way. Basically you put all the values into a single string, and then use string functions to do the work.
Code is commented with explanatory notes:
Demo: https://3v4l.org/i1WKI
Credit to Using str_replace so that it only acts on the first match? for code to replace only the first occurence of a string inside another string.
We can use an array to keep track of each item that has been removed, and then use array_shift to move out of the item and count to limit loop overruns