要使用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);