Jump to content

Remove all but last 10 elements in an array


Recommended Posts

I'm looking for a way to remove all but the last 10 elements in an array. Is it possible to do this with array_splice? I have an array with greater than 1000 elements and I don't want to have to specify each element I want to remove. It would be cool if I could do something like "remove elements 1-990".

Link to comment
Share on other sites

user array_slice() like array_slice($array,0,-10)


Thanks. That worked.


$a = [ "1" => "test1", "2" => "test2", "3" => "test3", "4" => "test4", "5" => "test5", "6" => "test6", "7" => "test7", "8" => "test8", "9" => "test9", "10" => "test10", "11" => "test11", "12" => "test12", "13" => "test13", "14" => "test14", "15" => "test15", "16" => "test16", "17" => "test17", "18" => "test18", "19" => "test19", "20" => "test20" ];
array_splice($a, 1, -10);
Edited by bruceblacklaws
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.