温馨提示×

PHP中的shift函数如何工作

PHP
小樊
83
2024-08-29 12:20:57
栏目: 编程语言

在 PHP 中,shift 函数实际上是指 array_shift() 函数

array_shift() 函数用于从数组中移除第一个元素,并返回该元素的值。所有其他元素的索引将减小 1。如果数组为空,则返回 NULL。

以下是使用 array_shift() 函数的示例:

<?php
$fruits = array("apple", "banana", "cherry");
$first_fruit = array_shift($fruits);

echo "The first fruit is: " . $first_fruit . "\n";
print_r($fruits);
?>

输出结果:

The first fruit is: apple
Array
(
    [0] => banana
    [1] => cherry
)

可以看到,“apple” 被移除,数组中剩余的元素索引减小了 1。

0