In PHP, array is a very common data structure. We can store and manage a series of values through arrays. However, in actual development, we may encounter situations where we need to remove a key-value pair in the array. So, how to remove key-value pairs from an array in PHP? In this article, we will detail how to remove keys from arrays in PHP.
1. Use the unset() function to remove a key-value pair from an array
In PHP, we can use the unset() function to remove a key-value pair from an array. The syntax of the unset() function is relatively simple, as follows:
unset($array['key']);
Among them, $array is the array to be operated on, and 'key' is The key to remove.
The following is a sample code:
// 声明一个包含键值对的数组 $array = array('name' => 'Tom', 'age' => 20, 'city' => 'Beijing'); // 移除数组中的 'city' 键值对 unset($array['city']); // 输出移除后的数组 print_r($array);
The running results are as follows:
Array ( [name] => Tom [age] => 20 )
As can be seen from the output results, with the help of the unset() function, we successfully moved Except for the 'city' key-value pair in the array.
2. Use the array_diff_key() function to remove multiple array key-value pairs
If we need to remove multiple key-value pairs at one time, we can use the array_diff_key() function. This function is used to calculate the difference of an array, and the returned result is an array containing the difference elements. The specific syntax is as follows:
array_diff_key($array1, $array2, ...);
Among them, $array1, $array2, etc. are the arrays to be compared. Specifically, the function returns the key-value pairs in $array1 that are not in the subsequent array. The following is a sample code:
// 声明一个包含键值对的数组 $array1 = array('name' => 'Tom', 'age' => 20, 'city' => 'Beijing'); // 声明另一个数组,用于指定要移除的键 $array2 = array('city' => ''); // 移除 'city' 键值对 $result = array_diff_key($array1, $array2); // 输出移除后的数组 print_r($result);
The running results are as follows:
Array ( [name] => Tom [age] => 20 )
As can be seen from the output results, the 'city' key value in $array1 was successfully removed using the array_diff_key() function right.
3. Use the array_splice() function to remove the array element at the specified position
In addition to directly removing a key-value pair, we can also use the array_splice() function to remove the array element at the specified position. This function is used to remove and replace a section of elements in an array. The specific usage is as follows:
array_splice($array, $offset, $length, $replacement);
where $array is the array to be operated on; $offset is the position of the first element to be removed/replaced. (Counting starts from 0); $length is the number of elements to be removed/replaced; $replacement is the element to be inserted. If you only want to remove elements at a certain position, set $length to 1 and $replacement to an empty array.
The following is a sample code:
// 声明一个包含多个元素的数组 $array = array('Tom', 'Bob', 'Lily', 'Kate', 'Jack'); // 移除第三个元素 'Lily' array_splice($array, 2, 1); // 输出移除后的数组 print_r($array);
The running results are as follows:
Array ( [0] => Tom [1] => Bob [2] => Kate [3] => Jack )
As can be seen from the output results, the array_splice() function is used to successfully remove the 'Lily' element.
Summary
Arrays are a common data type in PHP. To remove a key-value pair in the array, you can use the unset() function; to remove multiple key-value pairs, You can use the array_diff_key() function; to remove elements at a specified position in the array, you can use the array_splice() function. The usage of these functions is relatively simple. Once mastered, arrays can be easily manipulated and development efficiency improved.
The above is the detailed content of How to remove keys from php array. For more information, please follow other related articles on the PHP Chinese website!