1.array_unshift()是将元素放入数组的开头。
array_unshift — 在数组开头插入一个或多个单元
举例:
<?php
$queue = array("orange", "banana");
array_unshift($queue, "apple", "raspberry");
print_r($queue);
?>
以上例子会输出:
Array ( [0] => apple [1] => raspberry [2] => orange [3] => banana )2.与之对应的有array_shift()方法
array_shift — 将数组开头的单元移出数组
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>
以上例程会输出:
Array ( [0] => banana [1] => apple [2] => raspberry )
array_pop — 将数组最后一个单元弹出(出栈)
<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);
?>
经过此操作后,$stack 将只有 3 个单元:
Array ( [0] => orange [1] => banana [2] => apple )