Home >PHP Framework >ThinkPHP >Summarize several common ways to delete array fields in thinkphp
In ThinkPHP, deleting a specified field in an array is a common operation. However, since PHP is a dynamic language, some developers may not be familiar with the syntax of some array operations, resulting in code that is not elegant and efficient enough. This article will introduce several common methods of deleting array fields so that developers can write more concise, elegant, and efficient code.
The unset() function in PHP can delete specified elements in an array. The following is an example that shows how to use the unset() function to delete a specified field in the array:
$arr = array('id' => 1, 'name' => 'Tom', 'age' => 20); unset($arr['age']); print_r($arr);
Execute the code, the results are as follows:
Array ( [id] => 1 [name] => Tom )
Through the unset() function, we succeeded Removed 'age' field in array $arr.
Although this method is simple, its shortcomings are also obvious: you must first know the key name of the field to be deleted, which may be quite difficult for a large, deeply nested array.
array_diff_key() function can compare the key names of two arrays and return a new array that contains all the keys in the A key that appears in one array but not in other arrays. In short, it compares the key names of the two arrays and filters out those key names that only appear in the first array.
The following is an example of using the array_diff_key() function:
$arr = array('id' => 1, 'name' => 'Tom', 'age' => 20); $arr2 = array('id' => 1, 'name' => 'Tom'); $newarr = array_diff_key($arr, $arr2); print_r($newarr);
Execute the code and the results are as follows:
Array ( [age] => 20 )
Through this example, we see that using array_diff_key() function, we can compare the key names of two arrays without directly operating the array, find those key names that only appear in the first array, and then return a new array that contains all the unused keys. Key names that appear in other arrays.
In addition to using the array_diff_key() function to delete certain key names in the array, we can also use the array_filter() function. This function is used to filter elements in an array and return a filtered array. We can use the second parameter in the array_filter() function to specify the elements we wish to filter. The following is an example of using the array_filter() function:
$arr = array('id' => 1, 'name' => 'Tom', 'age' => 20); $newarr = array_filter($arr, function($k) { return $k !== 'age'; }, ARRAY_FILTER_USE_KEY); print_r($newarr);
Execute the code and the results are as follows:
Array ( [id] => 1 [name] => Tom )
Through this example, we see that using the array_filter() function, we can specify a callback function and in the callback function, judge the elements. If the judgment result is TRUE, the element is returned to the new array, otherwise it is not returned. In this example, we use the callback function to filter out the elements with the key name 'age' in the array.
The last method is to use the array_map() function. array_map() is a very practical array function in PHP. It is used to apply a callback function to each element of a given array and return a new array containing all return values. When we need to process each value in an array, we can consider using the array_map() function.
The following is an example of using the array_map() function:
$arr = array('id' => 1, 'name' => 'Tom', 'age' => 20); $newarr = array_map(function($key) use ($arr) { unset($arr[$key]); return $arr; }, array('age')); print_r($newarr);
Execute the code, the results are as follows:
Array ( [0] => Array ( [id] => 1 [name] => Tom ) )
In this example, we use an anonymous function, Apply it to the array ['age'], use the unset() function to delete the element with the key name 'age' from the array, and return the processed array to the new array.
Conclusion
According to different situations, we can use the above methods to delete fields in the array. If we are deleting a fixed element, we can use the unset() function; if we only need to delete some specified elements, we can use the array_diff_key() function; if we need to perform complex filtering and screening of elements, we can use array_filter () and array_map() functions. In short, according to our actual needs, we can choose a suitable method to delete specified elements in the array.
The above is the detailed content of Summarize several common ways to delete array fields in thinkphp. For more information, please follow other related articles on the PHP Chinese website!