Skip to content
Advertisement

How can I eliminate repeated elements but leaving the last element in the array PHP

Well, I need to be able to eliminate the repeated elements but keeping the last element, that is, eliminate the previous ones and leave the last one.

This is what I have

JavaScript

I have to delete the previous ones.

JavaScript

and leave this

JavaScript

And this is what I need

JavaScript

I need to keep the last one and delete the previous ones

Try this but delete the last one not the previous ones.

JavaScript

Advertisement

Answer

You could simply override the value of the current element using the same key. In this case, you will always get the last element for each ID

JavaScript

Output:

JavaScript

live demo

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