PHP Add new once element in array and remove last once -
i want keep 3 element in array. so, want if new element added in array oldest element remove array , re-index array. best , fast way make it?
this first array
$foo = array( 'when', // [0] 'whom', // [1] 'what' // [2] );
if new element "how" added, want modify below.
$foo = array( 'how', // [0] 'what', // [1] 'whom' // [2] );
php has many built in array functions including array_unshift
allows add element beginning , array_pop
allows remove element end:
array_unshift($foo, 'how'); // add beginning array_pop($foo); // remove end
example: https://eval.in/859692
Comments
Post a Comment