使用 unset 函数从数组中删除元素的 PHP 程序

phpserver side programmingprogramming更新于 2025/8/23 2:52:17

要使用 unset 函数从数组中删除元素,PHP 代码如下 −

示例

<?php
   $my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");
   unset($my_array[4]);
   print_r("After deleting the element, the array is");
   print_r ($my_array);
?>

输出

After deleting the element, the array isArray (
   [0] => Joe [1] => Ben [2] => Mary [3] => Barun [5] =>
   Mona
)

上面定义了一个数组,其中包含多个字符串 −

$my_array = array("Joe", "Ben", "Mary", "Barun", "Sona", "Mona");

使用 unset 函数,指定数组中需要删除的元素的索引。删除元素后,输出/数组将打印在屏幕上。−

unset($my_array[4]);
print_r("After deleting the element, the array is");
print_r ($my_array);

相关文章