我知道您可以使用“array_pop”删除数组中的最后一个元素。但是,如果我想删除最后 2 或 3 个,我该怎么办?
那么我该如何删除这个数组中的最后两个元素呢?
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
最佳答案
使用array_splice并指定要删除的元素数。
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_splice($stack, -2);
print_r($stack);
输出
数组
(
[0] => orange
[1] => banana
)
关于php - 如何从数组末尾删除第n个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20621345/