$v){//loop body statement block;}"; 2. In the loop In the body, use the unset() function to delete the specified element in the two-dimensional array according to the outer element key name "$k" and the field name. The syntax "unset($two-dimensional array variable name[$k][specified field name]);"."/> $v){//loop body statement block;}"; 2. In the loop In the body, use the unset() function to delete the specified element in the two-dimensional array according to the outer element key name "$k" and the field name. The syntax "unset($two-dimensional array variable name[$k][specified field name]);".">
Home > Article > Backend Development > How to remove elements of a certain field from a two-dimensional array in PHP
Removal steps: 1. Use the foreach statement to traverse the outer elements of the two-dimensional array, the syntax "foreach ($two-dimensional array variable name as $k => $v){//Loop body statement block; }"; 2. In the loop body, use the unset() function to delete the specified element in the two-dimensional array based on the outer element key name "$k" and the field name. The syntax "unset($ two-dimensional array variable name [$k][Specify field name]);".
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 elements of the specified field (column) in the two-dimensional array.
Implementation steps:
Step 1:Use the foreach statement to traverse the outer elements of the two-dimensional array
foreach ($二维数组变量名 as $k => $v){ //循环体语句块; }
Traverses the given array, and in each loop, the value of the current array is assigned to $v, and the key name is assigned to $k.
Step 2: In the loop body, use the unset() function to delete the specified element in the two-dimensional array based on the outer element key name $k and field name
unset($二维数组变量名[$k][指定字段名]);
$Two-dimensional array variable name [outer subscript][inner subscript]
You can access the specified elements of the two-dimensional array.
Complete sample code:
<?php header('content-type:text/html;charset=utf-8'); $arr=array( array( 'id' => 1, 'name' => "小明", 'score' => 85 ), array( 'id' => 2, 'name' => "小华", 'score' => 92 ), array( 'id' => 3, 'name' => "霄晓", 'score' => 100 ), array( 'id' => 4, 'name' => "萧洁", 'score' => 99 ), array( 'id' => 5, 'name' => "赵峰", 'score' => 96 ) ); var_dump($arr); $score=array_column($arr, 'score',"score"); foreach ($arr as $k1 => $v1){ unset($arr[$k1]["score"]); } echo "去掉二维数组中指定字段(列)的元素后:"; var_dump($arr); ?>
Recommended learning: "PHP Video Tutorial》
The above is the detailed content of How to remove elements of a certain field from a two-dimensional array in PHP. For more information, please follow other related articles on the PHP Chinese website!