Implementation steps: 1. Nest two foreach to traverse the key names and key values of the inner and outer layers of the two-dimensional array. The syntax is "foreach($arr as $k1=>$v2){foreach($v1 as $k2=>$v2){//Loop body statement block}}"; 2. In the loop body, use "===" to find the specified field name element, and use unset() to delete the element according to the corresponding key name. Syntax "if($k==="Specified field name"){unset($arr[$k1][$k2]);}".
The operating environment of this tutorial: Windows 7 system, PHP version 8.1, DELL G3 computer
In PHP, you can use the foreach statement and unset () function to remove the value of a field in a two-dimensional array.
Implementation steps:
Step 1: Nest two foreach statements to traverse the key names and keys of the inner and outer layers of the two-dimensional array Value
foreach ($array as $key => $value){ foreach ($value as $k => $v){ //内层循环体语句块; } }
The first foreach statement in the outer layer: Traverse the outer layer of the given $array array, and assign the value of the current array to $ in each loop value, the key name is assigned to $key.
The second foreach statement in the inner layer: traverses the $value subarray, and assigns the value of the current array to $v and the key name to $k in each loop.
Step 2: In the loop body, use "===" to find the element with the specified field name, and use the unset() function to delete the element according to the corresponding key name
if($k==="指定字段名"){ unset($arr[$key][$k]); }
Implementation code:
function f($arr,$s){ foreach ($arr as $key => $value){ foreach ($value as $k => $v){ if($k===$s){ unset($arr[$key][$k]); } } } echo "删除后的二维数组:"; var_dump($arr); }
Call f($arr) to process the following function and delete the name field or score field
$arr=array( array( 'name' => "小明", 'score' => 85, ), array( 'name' => "小华", 'score' => 92, ), array( 'name' => "霄晓", 'score' => 100, ), array( 'name' => "萧洁", 'score' => 99, ), array( 'name' => "赵峰", 'score' => 96, ) ); var_dump($arr); f($arr,"name");
f($arr,"score");
Explanation: foreach statement
foreach is a statement specially designed for traversing arrays. The method commonly used in arrays provides great convenience in traversing arrays; after PHP5, objects can also be traversed (foreach can only be applied to arrays and objects).
The foreach statement traverses the array regardless of the array subscript, and can be used for discontinuous index arrays and associative arrays with strings as subscripts.
The foreach statement has two syntax formats:
Syntax format 1:
foreach ($array as $value){ 语句块; }
Traverse the given $array array, in each loop Assign the value of the current array to $value.
Syntax format 2:
foreach ($array as $key => $value){ 语句块; }
Traverse the given $array array, and assign the value of the current array to $value, the key name is assigned to $key.
When the foreach statement loops, the pointer inside the array will move forward one step, so that the next array element will be obtained in the next loop until the end of the array is traversed and stopped. Traverse and exit the loop.
Recommended learning: "PHP Video Tutorial"
The above is the detailed content of How to remove the value of a field in a two-dimensional array in php. For more information, please follow other related articles on the PHP Chinese website!