$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 >Backend Development >PHP Problem >How to remove elements of a certain field from a two-dimensional array in PHP

How to remove elements of a certain field from a two-dimensional array in PHP

青灯夜游
青灯夜游Original
2022-07-28 18:59:072436browse

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]);".

How to remove elements of a certain field from a two-dimensional array in PHP

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(&#39;content-type:text/html;charset=utf-8&#39;);   
$arr=array(
  array(
    &#39;id&#39; => 1,
    &#39;name&#39; => "小明",
    &#39;score&#39; => 85
  ),
  array(
    &#39;id&#39; => 2,
    &#39;name&#39; => "小华",
    &#39;score&#39; => 92
  ),
  array(
    &#39;id&#39; => 3,
    &#39;name&#39; => "霄晓",
    &#39;score&#39; => 100
  ),
  array(
    &#39;id&#39; => 4,
    &#39;name&#39; => "萧洁",
    &#39;score&#39; => 99
  ),
  array(
    &#39;id&#39; => 5,
    &#39;name&#39; => "赵峰",
    &#39;score&#39; => 96
  )
);
var_dump($arr);
$score=array_column($arr, &#39;score&#39;,"score");
foreach ($arr as $k1 => $v1){
	
	    unset($arr[$k1]["score"]);

}
echo "去掉二维数组中指定字段(列)的元素后:";
var_dump($arr);
?>

How to remove elements of a certain field from a two-dimensional array in PHP

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn