Home  >  Article  >  Backend Development  >  How to delete elements in php two-dimensional array

How to delete elements in php two-dimensional array

PHPz
PHPzOriginal
2023-04-18 09:47:40797browse

In PHP, two-dimensional array is a very common data type, which can be used to represent data structures such as tables and matrices. When we need to delete certain elements from a two-dimensional array, we need to use some built-in functions and methods of PHP. In this article, we will introduce several methods to delete two-dimensional array elements in PHP.

Method 1: Use the unset() function

The unset() function in PHP can be used to delete one or more specified variables or array elements. If you want to delete elements in a one-dimensional array, you can use the unset() function directly, but if you want to delete elements in a two-dimensional array, we need to use another parameter of the unset() function to specify the element to be deleted.

The sample code is as follows:

<?php

// 定义一个二维数组
$arr = array(
    array(&#39;name&#39; => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22)
);

// 删除数组中的第二个元素
unset($arr[1]);

print_r($arr);

?>

Execute the above code and the output result is as follows:

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

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

)

As can be seen from the above code, we use the unset() function to delete the array The second element in (John Doe).

Method 2: Use array_splice() function

The array_splice() function in PHP can be used to delete specified elements in an array and return an array of deleted elements. It can also insert new elements into the array. In a two-dimensional array, we need to use two parameters to specify the elements to be deleted. The first parameter specifies the rows to delete, and the second parameter specifies the columns to delete.

The sample code is as follows:

<?php

// 定义一个二维数组
$arr = array(
    array(&#39;name&#39; => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22)
);

// 删除数组中的第二个元素
array_splice($arr, 1, 1);

print_r($arr);

?>

Execute the above code, and the output result will be the same as method one. In this example, we use the array_splice() function to delete the second element (John Doe) in the array.

Method 3: Use foreach loop and unset() function

In addition to the above two methods, we can also use foreach loop and unset() function to delete elements in the two-dimensional array. In this method, we need to use a foreach loop to traverse the array and delete the specified element using the unset() function.

The sample code is as follows:

<?php

// 定义一个二维数组
$arr = array(
    array(&#39;name&#39; => '张三', 'age' => 18),
    array('name' => '李四', 'age' => 20),
    array('name' => '王五', 'age' => 22)
);

// 删除数组中的第二个元素
foreach ($arr as $key => $value) {
    if ($key == 1) {
        unset($arr[$key]);
    }
}

print_r($arr);

?>

Execute the above code, and the output result will be the same as the first two methods. In this example, we used a foreach loop and the unset() function to delete the second element in the array (John Doe).

Summary:

In PHP, there are three ways to delete elements in a two-dimensional array: using the unset() function, using the array_splice() function, and using the foreach loop and unset() function. Each of these methods can effectively delete elements from a two-dimensional array, but which method to choose depends on the specific situation. If you only need to delete a few elements, then using the unset() function may be the easiest way; if you need to delete multiple elements and need to reorder the array, then using the array_splice() function may be more convenient; If you need to judge and delete each element in the array one by one, then using a foreach loop and the unset() function may be the most appropriate method.

The above is the detailed content of How to delete elements in php two-dimensional array. 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