Home Backend Development PHP Problem php two-dimensional array deletion

php two-dimensional array deletion

May 23, 2023 pm 12:05 PM

In PHP, deleting a two-dimensional array usually requires the use of loops and conditional statements. In actual programming, we usually encounter the following two situations where we need to delete a two-dimensional array:

  1. Delete a specified element: you need to find the subarray where the element to be deleted is located, and remove it from the subarray Delete the specified element.
  2. Delete the entire subarray: You need to find the parent array containing the subarray to be deleted, and delete the subarray.

Below we will introduce the specific implementation methods of these two types of tasks.

Method 1: Delete the specified element

To delete a specified element, we need to loop through all sub-arrays, find the sub-array containing the target element, and remove it from the sub-array delete. The following is a code example to implement this process:

// 定义一个二维数组
$arr = array(
    array('id'=>1, 'name'=>'Tom', 'age'=>20),
    array('id'=>2, 'name'=>'Jack', 'age'=>25),
    array('id'=>3, 'name'=>'Mary', 'age'=>30)
);

$target_key = "name"; // 要删除的元素名称
$target_value = "Jack"; // 要删除的元素值

// 遍历所有子数组
foreach($arr as $key=>$value){
    // 判断该子数组是否包含目标元素
    if(array_key_exists($target_key, $value) && $value[$target_key] == $target_value){
        // 如果找到了目标元素,就从该子数组中删除之
        unset($arr[$key][$target_key]);
    }
}

// 打印删除后的数组
print_r($arr);
Copy after login

After running the code, the output result is:

Array
(
    [0] => Array
        (
            [id] => 1
            [age] => 20
        )

    [1] => Array
        (
            [id] => 2
            [name] => Jack
            [age] => 25
        )

    [2] => Array
        (
            [id] => 3
            [name] => Mary
            [age] => 30
        )

)
Copy after login

As you can see, the code successfully changes the subarray named "Jack" to The "name" element is removed.

Method 2: Delete the entire subarray

In actual programming, we may encounter situations where we need to delete the entire subarray. For example, when processing database query results, we need to delete invalid records from the result set. The following is a code example to implement this process:

// 定义一个二维数组
$arr = array(
    array('id'=>1, 'name'=>'Tom', 'age'=>20),
    array('id'=>2, 'name'=>'Jack', 'age'=>25),
    array('id'=>3, 'name'=>'Mary', 'age'=>30)
);

$target_key = "id"; // 要删除的子数组中包含的元素名称
$target_value = 2; // 要删除的子数组中包含的元素值

// 遍历所有子数组
foreach($arr as $key=>$value){
    // 判断该子数组是否包含目标元素
    if(array_key_exists($target_key, $value) && $value[$target_key] == $target_value){
        // 如果找到了目标元素,就将整个子数组删除之
        unset($arr[$key]);
    }
}

// 打印删除后的数组
print_r($arr);
Copy after login

After running the code, the output is:

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => Tom
            [age] => 20
        )

    [2] => Array
        (
            [id] => 3
            [name] => Mary
            [age] => 30
        )

)
Copy after login

As you can see, the code successfully deletes the second subarray from the array.

Summary:

In PHP, deleting a two-dimensional array usually requires the use of loops and conditional statements. To delete the specified element, we need to find the subarray containing the target element and delete it from the subarray; to delete the entire subarray, we need to find the parent array containing the target subarray and delete the subarray. After mastering these basic methods, we can easily operate and manage two-dimensional arrays.

The above is the detailed content of php two-dimensional array deletion. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1664
14
PHP Tutorial
1268
29
C# Tutorial
1248
24