This question already has an answer here:
Is it possible to delete all array elements after an index?
$myArrayInit = array(1=>red, 30=>orange, 25=>velvet, 45=>pink);
now some "magic"
$myArray = delIndex(30, $myArrayInit);
to get
$myArray = array(1=>red, 30=>orange);
due to the keys in $myArray
are not successive, I don't see a chance for array_slice()
Please note
: Keys have to be preserved! + I do only know the Offset Key!!
I'd iterate over the array up until you reach the key you want to truncate the array thereafter, and add those items to a new - temporary array, then set the existing array to null, then assign the temp array to the existing array.
This uses a flag value to determine your limit:
Without making use of loops.
Output :
Try this:
This code is not tested
Try
See demo