Skip to content
Advertisement

PHP Merge two array with duplicates

I have two array.

JavaScript

My Result;

JavaScript

I would like to merge it with duplicates ids.

JavaScript

Advertisement

Answer

If your arrays are indeed this small, array_shift is a good tool. You can copy your array, pop an item off it continuously, and then re-copy the array as needed. The performance of array_shift gets worse as arrays grow large (as far as I’ve been told, but haven’t personally witnessed).

JavaScript

If you users is indeed linear like that, you can also use modular arithmetic to repeat things:

JavaScript

Both demoed here: https://3v4l.org/LuRQj

User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement