Home > Article > Backend Development > php delete specified in array
PHP is a server-side scripting language widely used in web development. In the process of using PHP for web development, it is often necessary to operate on arrays. Among them, deleting specified elements in an array is a common requirement. This article will introduce the deletion operation of arrays in PHP, including deleting specified elements, deleting specified key-value pairs, clearing the entire array, etc.
1. Delete the specified element
In PHP, you can use the unset function to delete the specified element in the array. This function accepts one or more parameters, each parameter is the position of the array element to be deleted. The following is an example of deleting a specified element in an array:
$fruits = array("apple", "banana", "cherry"); unset($fruits[1]); // 删除数组中第2个元素(即"banana") print_r($fruits); // 输出结果为:Array ( [0] => apple [2] => cherry )
In the above example, the unset function is used to delete the 2nd element ("banana") in the array. The final output result is the array ["apple", "cherry"].
If you want to delete multiple elements, you can pass multiple parameters in the unset function. For example:
$fruits = array("apple", "banana", "cherry", "date", "elderberry"); unset($fruits[1], $fruits[3]); // 删除数组中第2个和第4个元素(即"banana"和"date") print_r($fruits); // 输出结果为:Array ( [0] => apple [2] => cherry [4] => elderberry )
In the above example, the 2nd and 4th elements ("banana" and "date") in the array were deleted using the unset function. The final output result is the array ["apple", "cherry", "elderberry"].
2. Delete the specified key-value pair
In PHP, you can also use the unset function to delete the specified key-value pair in the array. This is done by passing the key name as a parameter. The following is an example of deleting a specified key-value pair:
$person = array("name" => "Tom", "age" => 20, "gender" => "male"); unset($person["age"]); // 删除数组中键为"age"的键值对 print_r($person); // 输出结果为:Array ( [name] => Tom [gender] => male )
In the above example, the unset function is used to delete the key-value pair with the key "age" in the array. The final output result is ["name" => "Tom", "gender" => "male"].
If you want to delete multiple key-value pairs, you can pass multiple key names as parameters in the unset function. For example:
$person = array("name" => "Tom", "age" => 20, "gender" => "male", "city" => "New York", "job" => "programmer"); unset($person["age"], $person["city"]); // 删除数组中键为"age"和"city"的键值对 print_r($person); // 输出结果为:Array ( [name] => Tom [gender] => male [job] => programmer )
In the above example, the unset function is used to delete the key-value pairs with the keys "age" and "city" in the array. The final output result is ["name" => "Tom", "gender" => "male", "job" => "programmer"].
3. Clear the entire array
If you want to clear the entire array, you can use the following two methods:
$fruits = array("apple", "banana", "cherry"); foreach ($fruits as $key => $value) { unset($fruits[$key]); } print_r($fruits); // 输出结果为:Array ( )
In the above example, a foreach loop is used to iterate through all the elements in the array and the unset function is used to delete these elements. The final output result is an empty array [].
$fruits = array("apple", "banana", "cherry"); $fruits = array(); // 使用空数组覆盖原数组 print_r($fruits); // 输出结果为:Array ( )
In the above example, the original array is overwritten with an empty array, so that all elements in the original array are cleared. The final output result is an empty array [].
Summary
This article introduces the deletion operation of arrays in PHP, including deleting specified elements, deleting specified key-value pairs, clearing the entire array, etc. In actual web development, array deletion operations are very common in many scenarios. Mastering these operations can help us better handle array-related business needs.
The above is the detailed content of php delete specified in array. For more information, please follow other related articles on the PHP Chinese website!