Skip to content
Advertisement

PHP get the last 3 elements of an associative array while preserving the keys?

I have an array:

JavaScript

How can I get the last 3 elements while preserving the keys? (the number of elements in the array may vary, so I cannot simply slice after the 2nd element)

So the output would be:

JavaScript

Advertisement

Answer

If you want to preserve key, you can pass in true as the fourth argument:

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