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

php two-dimensional array delete

WBOY
WBOYOriginal
2023-05-23 09:40:36512browse

In PHP, two-dimensional arrays are often used to store tabular data or store related data in the form of key-value pairs. But sometimes we need to delete certain data from a two-dimensional array, so this article will introduce a variety of methods to delete two-dimensional array data.

  1. array_splice() function

array_splice() function can delete a certain piece of data from the array and return the deleted element. Its usage is as follows:

array_splice ( array &$input , int $offset [, int $length = 0 [, mixed $replacement = array() ]] ) : array

Among them, the $input parameter is the array that needs to be operated; the $offset parameter is the index position to start deleting; the $length parameter is the number of elements to be deleted; the $replacement parameter is Used to replace deleted elements. If the $length parameter is not specified, the deleted elements will start at the $offset position and end at the end of the array. If the $replacement parameter is not specified, an array of deleted elements is returned.

When deleting an element in a two-dimensional array, we need to specify the position and quantity to be deleted, so you can use the following code:

$array = array(
    array('id' => 1, 'name' => 'A'),
    array('id' => 2, 'name' => 'B'),
    array('id' => 3, 'name' => 'C'),
    array('id' => 4, 'name' => 'D')
);
$index = 1; //要删除的索引位置
$len = 1; //要删除的元素个数
array_splice($array, $index, $len);

After executing the above code, the array $array will Modified to:

[
    ['id' => 1, 'name' => 'A'],
    ['id' => 3, 'name' => 'C'],
    ['id' => 4, 'name' => 'D']
]
  1. unset() function

The unset() function can be used to delete the element corresponding to the specified key name in the array:

unset($array[$key]);

When deleting an element in a two-dimensional array, we need to specify the key name to be deleted and the array in which it is located. For example, delete the record with id 2 in the following array:

$array = array(
    array('id' => 1, 'name' => 'A'),
    array('id' => 2, 'name' => 'B'),
    array('id' => 3, 'name' => 'C'),
    array('id' => 4, 'name' => 'D')
);
$key = null;
foreach ($array as $k => $v) {
    if ($v['id'] == 2) {
        $key = $k; //找到了要删除的键名
        break;
    }
}
if ($key !== null) {
    unset($array[$key]);
}

After executing the above code, the array $array will be modified to:

[
    ['id' => 1, 'name' => 'A'],
    ['id' => 3, 'name' => 'C'],
    ['id' => 4, 'name' => 'D']
]
  1. array_filter() function

array_filter() function can be used to filter elements in an array, that is, to delete elements in the array that do not meet the conditions. When the second parameter is not passed, elements with a value of false are deleted by default.

When deleting an element in a two-dimensional array, we need to use the callback function to perform judgment and filtering. For example, delete the record with id 2 in the following array:

$array = array(
    array('id' => 1, 'name' => 'A'),
    array('id' => 2, 'name' => 'B'),
    array('id' => 3, 'name' => 'C'),
    array('id' => 4, 'name' => 'D')
);
$array = array_filter($array, function($v) {
    return $v['id'] != 2; //返回id不等于2的记录
});

After executing the above code, the array $array will be modified to:

[
    ['id' => 1, 'name' => 'A'],
    ['id' => 3, 'name' => 'C'],
    ['id' => 4, 'name' => 'D']
]

Summary

This article Introduced various methods of deleting two-dimensional array data in PHP, including array_splice() function, unset() function and array_filter() function. Different methods can be selected according to different needs, and the specific implementation methods can also be optimized and improved according to the actual situation. Of course, when using any method, be sure to pay attention to the key name and index position of the array to avoid unnecessary errors.

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