溫馨提示×

PHP中的shift函數(shù)如何工作

PHP
小樊
83
2024-08-29 12:20:57
欄目: 編程語言

在 PHP 中,shift 函數(shù)實際上是指 array_shift() 函數(shù)

array_shift() 函數(shù)用于從數(shù)組中移除第一個元素,并返回該元素的值。所有其他元素的索引將減小 1。如果數(shù)組為空,則返回 NULL。

以下是使用 array_shift() 函數(shù)的示例:

<?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” 被移除,數(shù)組中剩余的元素索引減小了 1。

0