PHP: Remove the first and last item of the array

前端 未结 7 546
旧巷少年郎
旧巷少年郎 2021-01-31 07:47

Suppose I have this array:

 $array = array(\'10\', \'20\', \'30.30\', \'40\', \'50\');

Questions:

What is the fastest/

7条回答
  •  独厮守ぢ
    2021-01-31 08:11

    Removes the first element from the array, and returns it:

    array_shift($array);
    

    Removes the last element from the array, and returns it:

    array_pop($array);
    

    If you dont mind doing them both at the same time, you can use:

    array_shift($array,1,-1));
    

    to knock off the first and last element at the same time.

    Check the array_push, array_pop and array_slice documentation :)

提交回复
热议问题