I have a PHP array with elements:
$myarray = array ( "tom", "dick", "Harry" );
. I need to keep the array fixed in size of just 3 elements
. I need to add a new element “jerry” such that the array now looks like
$myarray = array ( "jerry", "tom", "dick");
so in a way I am moving the elements along and the 4th one drops out, with the newest element going at the beginning. I could write all of this by hand, renumbering the elements etc etc.
I just wondered if there was a quicker way to do this though.
Many thanks!
J
One way to do this is to utilize
array_popandarray_unshift:Or, you can use
array_mergeandarray_slice:Both of these methods reset the keys, so they will be renumbered from 0 to 2.