学堂 学堂 学堂公众号手机端

在PHP中,可以使用unset()函数来删除数组中的指定元素。下面是一个例子

lewis 1年前 (2024-04-21) 阅读数 12 #技术

在PHP中,可以使用unset()函数来删除数组中的指定元素。下面是一个例子:

$fruits=array("apple","banana","orange","grape"); //删除数组中的第二个元素(下标为1) unset($fruits[1]); //打印结果 print_r($fruits);

输出结果为:

Array ( [0]=>apple [2]=>orange [3]=>grape )

注意,使用unset()函数删除数组元素时,会保留原数组的索引。如果要重新索引数组,可以使用array_values()函数。例如:


$fruits=array_values($fruits); print_r($fruits);

输出结果为:

Array ( [0]=>apple [1]=>orange [2]=>grape )
版权声明

本文仅代表作者观点,不代表博信信息网立场。

热门