In PHP, two-dimensional array is a very commonly used data structure. When performing two-dimensional array operations, sometimes it is necessary to delete data in a certain column, and this situation occurs relatively frequently. So, how to delete the data of a certain column in a php two-dimensional array? This article will introduce this in detail.
Deletion method one: Use foreach loop to delete a certain column
In PHP, we can use foreach loop to traverse the two-dimensional array and delete the specified column during the traversal process. The specific ideas are as follows:
1. Take out each column of the array through a foreach loop.
2. Use the unset function to delete the specified column.
3. Reorganize the deleted columns into an array.
Code example:
$arr=array( array('id'=>1,'name'=>'张三','age'=>18), array('id'=>2,'name'=>'李四','age'=>19), array('id'=>3,'name'=>'王五','age'=>20) ); foreach($arr as &$v){ unset($v['name']); //删除某一列 } unset($v); //重置$v的引用 print_r($arr);
Delete method two: use array_map function to delete a certain column
In PHP, we can also use the array_map function to delete a certain column. The specific ideas are as follows:
1. Use the array_map function to operate on each element of the array (that is, each row) and delete the specified column.
2. Reconstruct the deleted array into a new array.
Code example:
$arr=array( array('id'=>1,'name'=>'张三','age'=>18), array('id'=>2,'name'=>'李四','age'=>19), array('id'=>3,'name'=>'王五','age'=>20) ); //回调函数:删除某一列 function delete_col($arr,$key){ unset($arr[$key]); return array_values($arr); } $new_arr=array_map('delete_col',$arr,array_fill(0,count($arr),'name')); print_r($new_arr);
Code description:
array_map() function is to call back a function for each element of the array and return a new array . Here, our callback function is the delete_col() function, which accepts two parameters: the array and the key of the column to be deleted (that is, the name column). In the function body, we use unset() to delete the specified column, then use array_values() to rearrange the keys of the array, and finally return the new array.
Delete method three: Use the array_column function to delete a certain column
In PHP, we can also use the array_column function to delete a certain column. The specific ideas are as follows:
1. First use the array_column function to obtain the value of the specified column.
2. Use the array_diff function to delete the obtained column value from the original array.
Code example:
$arr=array( array('id'=>1,'name'=>'张三','age'=>18), array('id'=>2,'name'=>'李四','age'=>19), array('id'=>3,'name'=>'王五','age'=>20) ); $column=array_column($arr,'name'); //获取指定列 $new_arr=array_map(function($v) use($column){ return array_diff_key($v,$column); //删除指定列 },$arr); print_r($new_arr);
Code description:
The array_column() function is to return a specified column in the array. Therefore, what is stored in the $column array is the name column in the two-dimensional array. Then, we use the array_map function to traverse the original array, use the array_diff_key function to delete the specified column from the original array, and finally return the new array.
Summary
For the operation of deleting a column in a PHP two-dimensional array, we can use the foreach loop, array_map function or array_column function to achieve it. In actual operation, we can choose the appropriate method according to the specific scenario to achieve efficient and simple results.
The above is the detailed content of How to delete a certain column of data from a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!