PHP:删除数组的第一项和最后一项 [英] PHP: Remove the first and last item of the array
本文介绍了PHP:删除数组的第一项和最后一项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有这个数组:
$array = array('10', '20', '30.30', '40', '50');
问题:
从上述阵列中删除第一项的最快/最简单方法是什么?
从上述阵列中删除最后一项的最快/最简单方法是什么?
What is the fastest/easiest way to remove the first item from the above array?
What is the fastest/easiest way to remove the last item from the above array?
因此,结果数组仅包含以下值:
So the resulting array contains only these values:
- '20'
- '30 .30'
- '40'
推荐答案
使用array_slice最简单
Using array_slice is simplest
$newarray = array_slice($array, 1, -1);
如果输入数组中的元素少于3个,则输出数组将为空.
If the input array has less than 3 elements in it, the output array will be empty.
这篇关于PHP:删除数组的第一项和最后一项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文