php two-dimensional array delete

WBOY
Release: 2023-05-23 09:40:36
Original
524 people have browsed it

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
Copy after login

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);
Copy after login

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

[
    ['id' => 1, 'name' => 'A'],
    ['id' => 3, 'name' => 'C'],
    ['id' => 4, 'name' => 'D']
]
Copy after login
Copy after login
Copy after login
  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]);
Copy after login

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]);
}
Copy after login

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

[
    ['id' => 1, 'name' => 'A'],
    ['id' => 3, 'name' => 'C'],
    ['id' => 4, 'name' => 'D']
]
Copy after login
Copy after login
Copy after login
  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的记录
});
Copy after login

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

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

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!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template