Jump to content

Remove all but last 10 elements in an array


bruceblacklaws

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".

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);
print_r($a);

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.