本文章向大家介绍php如何删除数组元素。文章一共使用到三种方法来删除数组元素,第一种方法是php unset()函数,第二种方法是php array_splice()函数,第三种方法是php array_diff()函数。
在PHP网络编程中,你可能需要删除数组中的一些元素,在本文章中,我们将演示并描述php如何删除数组元素。
文章一共列举了三种方法来实现这一功能:
php unset()函数
php array_splice()函数
php array_diff()函数
PHP unset()函数删除数组元素
如果要从数组中删除元素,则可以简单地使用该php
unset()
函数.
以下示例显示如何从关联数组和数字数组中删除元素。
$num_array = ["Amanda", "John", "Kelly", "Megan", "Henry"];
$asc_array = ["first" => "Amanda", "second" => "John",
"third" => "Kelly", "fourth" => "Megan",
"fifth" => "Henry"];
$key = array_search("John", $num_array);
unset($num_array[$key]);
print_r($num_array);