If I have two super-simple arrays, as such:
How do I combine them so that the result is a single array without duplicate values? i.e.,
I would have thought this could be handled with a simple function like or , but those operate on keys in an associative array. I need them to go by value. I don't care about the indices in the array and I can always sort it by value if I need to using one of the many sorting methods provided by PHP.
The same problem exists with Laravel's Collections: , , , , etc. don't work on values at all.
It seems incredibly inefficient to me that the only way to accomplish this is to write several lines, including a loop with conditions inside that use to decide whether or not to add each value from the second array to the first.
Is there a better way?
Edit 09/08/2022: Although this question was initially flagged as a duplicate of another one, the accepted answer to that question never actually answered the OP's question and, by extension, my own question of how to sort by value. In addition, the accepted answer of "use array_merge_recursive()" does not, by itself, answer my question. Finally, the question in that post involves an associative array, whereas my question disregards keys entirely. Thus, the reason why I posted this question and do not consider it a duplicate.
View on Stack Overflow
