Home  >  Article  >  Backend Development  >  How to delete key-value pairs in an array in php

How to delete key-value pairs in an array in php

PHPz
PHPzOriginal
2023-04-26 14:24:53506browse

In PHP development, arrays are one of the essential data types. Sometimes we need to delete certain key-value pairs and corresponding values ​​from the array. PHP provides several different functions to delete key-value pairs in an array. In this article, we will introduce ways to delete key-value pairs in an array in PHP.

  1. Use the unset() function

The unset() function is one of the most basic methods in PHP. It can completely delete a variable, including a key in the array. value pair. When deleting key-value pairs in the array, we only need to use the unset() function to reference the key to be deleted in the array. The following is a sample code that uses the unset() function to delete a key-value pair in an array:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
unset($fruits["banana"]);

In the above example, we used the unset() function and the array subscript reference to delete $fruits A key-value pair in the array, the result will be to delete the key-value pair "banana" => "yellow".

  1. Use the array_splice() function

The array_splice() function is also one of the methods used to delete key-value pairs in PHP arrays. It deletes an element from an array and inserts a value to fill the deleted position. When deleting a key-value pair, we only need to put the corresponding key into the first parameter of the function. The following is an example code for using the array_splice() function to delete a key-value pair in an array:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
array_splice($fruits, array_search("banana", array_keys($fruits)), 1);

In the above example, we used the array_splice() function, and first used the array_keys() function to obtain $fruits All the keys in the array, then use the array_search() function to locate the position of the "banana" key in the array, and finally use the array_splice() function to delete it.

  1. Using the array_filter() function

The array_filter() function can be used to delete certain key-value pairs in the array. It is implemented through a callback function, which is passed each key-value pair in the array in turn. If the return value is true, the key-value pair is retained, otherwise it is deleted. The following is an example code that uses the array_filter() function to delete a key-value pair in an array:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
$new_fruits = array_filter($fruits, function($key) { return $key != "banana" });

In the above example, we use the array_filter() function and an anonymous function callback to remove the " banana" key-value pair is deleted from the $new_fruits array.

  1. Use the unset() function combined with a loop

If you want to delete multiple key-value pairs in an array, the simplest way is to use a loop structure for each key Delete value pairs. As shown below:

$fruits = array("apple" => "red", "banana" => "yellow", "orange" => "orange");
$keys = array("banana", "orange");
foreach($keys as $key) {
    unset($fruits[$key]);
}

In the above example, we store the keys of the key-value pairs to be deleted in an array, and then use a loop structure to reference each key-value pair and use the unset() function Delete it.

Summary

There are many ways to delete array key-value pairs in PHP, including using the unset() function, array_splice() function, array_filter() function, and using unset in combination with loops () function and other methods. No matter which method is used, it can effectively delete specific key-value pairs in the PHP array. When deleting key-value pairs in an array, you also need to keep in mind the nature of the array index to ensure that the operation is correct.

The above is the detailed content of How to delete key-value pairs in an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn