PHP is a very popular server-side scripting language because of its ability to easily process data and provides a rich library of functions and tools so that developers can easily build complex websites and applications. One very common operation is to delete a key in an array, rather than just deleting an array element.
In PHP, the method of deleting a key in an array depends on the specific scenario, but below we will introduce three common methods.
Method 1: Use the unset() function
The easiest way is to use PHP's built-in function unset(). This function can delete the specified element in the array by passing in the key name to be deleted.
The following is an example:
<?php $array = array("name" => "John", "age" => 30, "email" => "john@example.com"); unset($array["email"]); print_r($array); ?>
The output is:
Array ( [name] => John [age] => 30 )
In this example, we use the unset() function to delete the "email" element in the array. Use the print_r() function to output the result of the array. You can see that the array no longer contains the deleted elements.
Method 2: Use the array_filter() function
If you want to delete multiple array elements, or want to use a reusable filter, then using the array_filter() function is more appropriate . The array_filter() function uses a custom callback function to filter array elements and delete elements that do not meet the conditions.
Here is an example:
<?php $array = array("name" => "John", "age" => 30, "email" => "john@example.com", "phone" => ""); $array = array_filter($array, function($value) { return $value !== ""; }); print_r($array); ?>
The output is:
Array ( [name] => John [age] => 30 [email] => john@example.com )
In this example, we define a callback function that will remove all empty strings from the array Elements. We pass the $array array to the array_filter() function and it will return a new array with all empty string elements removed.
Method 3: Use the array_diff_key() function
The last method is to use the array_diff_key() function. This function will remove an element from one array at a specified key in another array.
The following is an example:
<?php $array1 = array("name" => "John", "age" => 30, "email" => "john@example.com", "phone" => ""); $array2 = array_flip(array("email", "phone")); $array3 = array_diff_key($array1, $array2); print_r($array3); ?>
The output is:
Array ( [name] => John [age] => 30 )
In this example, we use the array_diff_key() function to delete "email" and "email" in the $array1 array "phone" element. We first convert the key to be deleted into a new array using the array_flip() function and then pass it to the array_diff_key() function.
Conclusion
All three methods can be used to delete a key in the array, and the specific use depends on your specific scenarios and requirements. In the case of using the unset() function to delete a single element, it is recommended to use the isset() function to check whether the key to be deleted exists to avoid accidentally deleting undefined keys.
The above is the detailed content of How to delete the same key value in php array. For more information, please follow other related articles on the PHP Chinese website!