Home >Backend Development >PHP Problem >Example to explain how to delete a subarray in a two-dimensional array in PHP

Example to explain how to delete a subarray in a two-dimensional array in PHP

PHPz
PHPzOriginal
2023-04-14 20:03:11773browse

In PHP, if an array contains multiple arrays, we call it a two-dimensional array. In some cases, we need to delete one or more sub-arrays from a two-dimensional array. Next, this article will explain how to use PHP to delete a subarray in a two-dimensional array.

Implementation method:

In PHP, there are many methods to delete elements in a two-dimensional array. However, in order to ensure the efficiency of the program and the simplicity of the code, we will use the following two methods to delete subarrays in the two-dimensional array.

1. Use the unset() function to delete two-dimensional array elements

In PHP, the unset() function is used to cancel the value of a variable, thereby releasing the memory occupied by the variable. This function can also be used to delete elements from an array. The following is a code example:

$arr = array(
    array('name' => '张三', 'age' => 20),
    array('name' => '李四', 'age' => 22),
    array('name' => '王五', 'age' => 24),
    array('name' => '赵六', 'age' => 26)
);

unset($arr[1]);

print_r($arr);

Output result:

Array
(
    [0] => Array
        (
            [name] => 张三
            [age] => 20
        )

    [2] => Array
        (
            [name] => 王五
            [age] => 24
        )

    [3] => Array
        (
            [name] => 赵六
            [age] => 26
        )
)

In the above example, we use the unset() function to delete the array element corresponding to $arr[1]. Note that array subscripts start counting from 0.

2. Use the array_diff() function to delete two-dimensional array elements

The array_diff() function can find the difference between two or more arrays. When we delete one or more elements in an array, we can use this function to delete.

$arr = array(
    array('name' => '张三', 'age' => 20),
    array('name' => '李四', 'age' => 22),
    array('name' => '王五', 'age' => 24),
    array('name' => '赵六', 'age' => 26)
);

$value = array('name' => '李四', 'age' => 22);

$result = array_diff($arr, array($value));

print_r($result);

Output result:

Array
(
    [0] => Array
        (
            [name] => 张三
            [age] => 20
        )

    [1] => Array
        (
            [name] => 王五
            [age] => 24
        )

    [2] => Array
        (
            [name] => 赵六
            [age] => 26
        )
)

In the above example, we created a $value array and used the array_diff() function to delete it from the $arr array.

Summary

This article introduces two methods of deleting two-dimensional array elements in PHP. Use the unset() function to delete specified array elements simply and quickly. Using the array_diff() function provides better flexibility when multiple elements need to be removed from an array.

No matter which method is chosen, both methods can achieve the desired results, and both are very commonly used. After understanding these techniques, we can better deal with two-dimensional arrays.

The above is the detailed content of Example to explain how to delete a subarray in 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