Home > Article > Backend Development > How to remove specified key value from php array
In PHP, array is a very common data type, which is usually used to store a set of related data. When processing arrays, sometimes we need to remove specified key values in the array, which is very common in actual development. This article will introduce several common methods to help you easily remove specified key values from PHP arrays.
Method 1: Use the unset() function
The unset() function can delete the specified variable, including the key value in the array. We can loop through the array and use the unset() function to delete the specified key value, as in the following example:
// 定义一个数组 $arr = array('apple', 'banana', 'cherry', 'dates'); // 删除指定的键值 unset($arr[1]); // 删除数组中的第二个元素 banana // 输出数组 print_r($arr);
The running result is:
Array ( [0] => apple [2] => cherry [3] => dates )
Method 2: Use the array_splice() function
array_splice() function is an array operation function provided by PHP. It can insert or delete elements in the array and return the deleted elements. We can use this function to delete the specified key value, as in the following example:
// 定义一个数组 $arr = array('apple', 'banana', 'cherry', 'dates'); // 删除指定的键值 array_splice($arr, 1, 1); // 删除数组中的第二个元素 banana // 输出数组 print_r($arr);
The running result is:
Array ( [0] => apple [1] => cherry [2] => dates )
When using the array_splice() function, the first parameter is an array, and the second The parameter is the starting position to be deleted, and the third parameter specifies the number of elements to be deleted.
Method 3: Use the array_diff() function
array_diff() function can be used to calculate the difference set of arrays, that is, return an array that contains the values that exist in array A but are not in array B. The element does not exist. When we want to remove one or more specified key values, we can encapsulate these key values into an array and pass it as the second parameter to the array_diff() function, the code is as follows:
// 定义一个数组 $arr = array('apple', 'banana', 'cherry', 'dates'); // 删除指定的键值 $rm_key = array(1); // 要删除的键值 banana $new_arr = array_diff($arr, array_intersect_key($arr, array_flip($rm_key))); // 输出数组 print_r($new_arr);
The running result is:
Array ( [0] => apple [2] => cherry [3] => dates )
In the code, the array_intersect_key() function is used to obtain the corresponding array of the key to be deleted, and the array_flip() function is used to exchange the key value and index position of the array. to get the corresponding key. Finally, we can pass the result of the above operation as a parameter to the array_diff() function to remove the specified key value.
Method 4: Use array_filter() function
array_filter() function can be used to filter array elements and return elements that meet specified conditions. We can set the key values to be deleted as filter conditions, and use the array_filter() function to remove the specified key values. The code is as follows:
// 定义一个数组 $arr = array('apple', 'banana', 'cherry', 'dates'); // 删除指定的键值 $rm_key = array(1); // 要删除的键值 banana $new_arr = array_filter($arr, function($k) use($rm_key) { return !in_array($k, $rm_key); }, ARRAY_FILTER_USE_KEY); // 输出数组 print_r($new_arr);
The running result is:
Array ( [0] => apple [2] => cherry [3] => dates )
In the code, we use an anonymous function as the callback function of the array_filter() function to filter the key values to be deleted. Among them, the use keyword is used to introduce external variables to ensure that the $rm_key variable can be accessed inside the loop. ARRAY_FILTER_USE_KEY means that we will filter the keys of the array.
To sum up, we have introduced four common methods to remove specified key values in PHP arrays. They are the unset() function, array_splice() function, array_diff() function and array_filter() function. . I hope this article can help you better handle PHP array-related operations.
The above is the detailed content of How to remove specified key value from php array. For more information, please follow other related articles on the PHP Chinese website!