Home  >  Article  >  Backend Development  >  php two-dimensional array deletion

php two-dimensional array deletion

王林
王林Original
2023-05-23 12:05:07550browse

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);

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
        )

)

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);

After running the code, the output is:

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

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

)

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:
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