Home > Article > Backend Development > How to exclude array keys using PHP
In PHP programming, arrays are a very common data type, and arrays often need to be operated on. However, sometimes we need to exclude certain keys from an array while retaining others. This article will explain how to exclude array keys using PHP.
1. Use the unset function
The unset function can delete a key-value pair. If a key is passed as a parameter to the unset function, the key will be deleted.
The following is an example of using the unset function to exclude an array key:
$my_array = array( 'foo' => 'bar', 'john' => 'doe', 'mary' => 'jane' ); unset($my_array['foo']); print_r($my_array); // Array ( [john] => doe [mary] => jane )
In the above example, the key 'foo' in the array $my_array is deleted through the unset function. We can see There is no value for the 'foo' key in the printed array.
Therefore, we can use the unset function to delete them one by one by looping through all the keys to be excluded in the array.
The following is an example of using the unset function to exclude multiple array keys:
$my_array = array( 'foo' => 'bar', 'john' => 'doe', 'mary' => 'jane', 'jane' => 'smith' ); $exclude_keys = array('foo', 'mary'); foreach ($exclude_keys as $key) { unset($my_array[$key]); } print_r($my_array); // Array ( [john] => doe [jane] => smith )
In the above example, a foreach loop is used to traverse the key array $exclude_keys to be excluded, and then the unset function is used They are removed from the array in turn.
2. Use the array_diff_key function
PHP provides a very convenient function array_diff_key, which can be used to compare the keys of two arrays and return the difference.
The following is an example of using the array_diff_key function to exclude multiple array keys:
$my_array = array( 'foo' => 'bar', 'john' => 'doe', 'mary' => 'jane', 'jane' => 'smith' ); $exclude_keys = array('foo', 'mary'); $filtered_array = array_diff_key($my_array, array_combine($exclude_keys, array_fill(0, count($exclude_keys), null))); print_r($filtered_array); // Array ( [john] => doe [jane] => smith )
In the above example, the array_combine and array_fill functions are used to generate an equal-length array with all null values. , and then use the array_diff_key function to return the difference between $my_array and the array whose keys are all empty, that is, all keys in the $exclude_keys array are excluded.
3. Use the array_filter function
In PHP, use the array_filter function to filter the array and return the corresponding array elements. Since it provides a callback function, we can write a callback function to determine whether a specific key needs to be excluded.
The following is an example of using the array_filter function to exclude multiple array keys:
$my_array = array( 'foo' => 'bar', 'john' => 'doe', 'mary' => 'jane', 'jane' => 'smith' ); $exclude_keys = array('foo', 'mary'); $filtered_array = array_filter($my_array, function($key) use ($exclude_keys) { return !in_array($key, $exclude_keys); }, ARRAY_FILTER_USE_KEY); print_r($filtered_array); // Array ( [john] => doe [jane] => smith )
In the above example, we define an anonymous function and use the use keyword to pass the $exclude_keys array to it. The function of this anonymous function is that if an array key exists in the $exclude_keys array, it returns false, indicating that it needs to be filtered out. Finally, we pass this anonymous function as a callback function to the array_filter function. When using the array_filter function, we specify the third parameter as ARRAY_FILTER_USE_KEY, indicating that we want to use the callback function to filter the keys of the array.
4. Use array_keys and array_diff functions
Finally, we can also use array_keys and array_diff functions to exclude array keys. The specific method is to first use the array_keys function to get all the keys of the array, then use the array_diff function to get the keys that need to be retained, and use these keys to construct a new array.
The following is an example of using array_keys and array_diff functions to exclude multiple array keys:
$my_array = array( 'foo' => 'bar', 'john' => 'doe', 'mary' => 'jane', 'jane' => 'smith' ); $exclude_keys = array('foo', 'mary'); $filtered_keys = array_diff(array_keys($my_array), $exclude_keys); $filtered_array = array_intersect_key($my_array, array_combine($filtered_keys, $filtered_keys)); print_r($filtered_array); // Array ( [john] => doe [jane] => smith )
In the above example, first use the array_keys function to obtain all the keys of $my_array, and then use the array_diff function Filter out the keys that need to be retained. Then use the array_combine function to form the filtered keys into an array with the same key values, and finally use the array_intersect_key function to return the filtered array.
Summary
PHP provides multiple methods to exclude array keys, and we can choose to use different methods to deal with different actual situations. Specifically, we can use the unset function, array_diff_key function, array_filter function, array_keys and array_diff functions to complete this operation. It is important to note that when using these methods, we need to handle array keys carefully to avoid accidentally deleting or retaining unnecessary keys.
The above is the detailed content of How to exclude array keys using PHP. For more information, please follow other related articles on the PHP Chinese website!