search
HomeBackend DevelopmentPHP Problemphp two-dimensional array delete

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment