使用 unset 函式從陣列中刪除元素的 PHP 程式
要使用 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);
廣告