Home >Backend Development >PHP Problem >How to delete a subarray in a two-dimensional array in php

How to delete a subarray in a two-dimensional array in php

PHPz
PHPzOriginal
2023-04-23 17:49:17762browse

In PHP programming, arrays are often used. An array is an ordered collection of values, where each value has a unique key. A two-dimensional array means that the values ​​in an array are also an array, that is, the elements in the array are also arrays. When dealing with two-dimensional arrays we often need to delete certain array elements in the array. Next, let's learn how to delete array elements in a two-dimensional array in PHP.

  1. Delete the array element with the specified key name

To delete the array element with the specified key name in the two-dimensional array, you can use the unset() function in PHP. This function receives one or more variables as parameters and deletes the values ​​of these variables from memory. When deleting array elements, you can use the array key name as a parameter. Here is an example:

$arr = [
    ['id' => 1,'name' => '张三'],
    ['id' => 2,'name' => '李四'],
    ['id' => 3,'name' => '王五']
];
unset($arr[1]);

In this example, we are going to delete the array element with the key name 1 in the array $arr. After executing unset($arr[1]), the value of $arr becomes:

[
    ['id' => 1,'name' => '张三'],
    ['id' => 3,'name' => '王五']
]

When deleting elements in the array through the unset() function, you need to pay attention to the following points:

  • Deleting array elements has no return value. The unset() function only deletes the value of the corresponding variable.
  • If you want to delete multiple array elements, you can use an unset() function statement to delete multiple variables, which can reduce program execution time and performance consumption.
  • If the key name in front of the array element is deleted, the subsequent key names will not be reordered. In other words, if an array has some consecutive integer key names, deleting a key name in the middle will not change the key names of other elements.
  1. Use the array_splice() function to delete array elements

PHP provides the array_splice() function to delete two-dimensional array elements. The array_splice() function is used to insert, delete and replace elements in an array. The basic syntax is:

array_splice(array,start,length,array)
  • array: required. The original array to operate on.
  • start: required. Specifies the position from which to delete elements.
  • length: optional. Specifies how many elements to delete. If omitted, all elements starting at start position to the end of the array are removed.
  • array: optional. The element inserted into the array at position start.

Here is an example:

$arr = [
    ['id' => 1,'name' => '张三'],
    ['id' => 2,'name' => '李四'],
    ['id' => 3,'name' => '王五']
];
array_splice($arr, 1, 1);

In this example, we are going to delete the array element with index 1 in the array $arr. After executing array_splice($arr, 1, 1), the value of $arr becomes:

[
    ['id' => 1,'name' => '张三'],
    ['id' => 3,'name' => '王五']
]

When deleting array elements in a two-dimensional array through the array_splice() function, you need to pay attention to the following points:

  • When deleting elements, the deletion position and deletion quantity must be specified. If the length parameter is not passed, all elements from the specified position to the end of the array will be deleted by default.
  • After deleting elements, the original array may be changed, so use it with caution. If the element you want to remove does not exist, no action will be performed and no error will be thrown.
  • When deleting elements, the index will be reordered. You can sort the array first and then delete it to ensure that the deleted array is still in order.
  1. Use the array_diff() function to delete array elements

The array_diff() function is also provided in PHP to delete specified array elements in a two-dimensional array. The array_diff() function is used to compare the difference between two or more arrays and return the difference between the two arrays. The basic syntax is:

array_diff(array1,array2,array3...)
  • array1: required. The first array to be compared.
  • array2: required. The second array to be compared.
  • array3...: Optional. Other arrays that need to be compared.

The following is an example:

$arr = [
    ['id' => 1,'name' => '张三'],
    ['id' => 2,'name' => '李四'],
    ['id' => 3,'name' => '王五']
];
$remove = [
    ['id' => 2,'name' => '李四']
];
$result = array_diff($arr, $remove);

In this example, we will delete the same array elements in the array $arr and the $remove array, execute array_diff($arr, $ remove), the value of the $result array becomes:

[
    ['id' => 1,'name' => '张三'],
    ['id' => 3,'name' => '王五']
]

When deleting elements in the two-dimensional array through the array_diff() function, you need to pay attention to the following points:

  • array_diff( ) function will return a new array of the difference between the two arrays. Only the different elements in the two arrays will be retained in the new array, and the identical elements will be deleted.
  • array_diff() function cannot compare elements by value and can only match by key name. Therefore, when using it, you need to ensure that the key name of the array is correct.
  • If you need to compare the differences in multiple arrays, you can pass multiple arrays to the array_diff() function and perform corresponding operations respectively.

Summary

There are many ways to delete array elements in a two-dimensional array in PHP. We can choose the solution that suits us best. When using the unset() function, array_splice() function, and array_diff() function to delete array elements, you need to pay attention to the differences between different functions and operations, as well as issues such as array operations and order changes. When writing PHP code, it is recommended to choose an appropriate method to delete two-dimensional array elements based on specific usage scenarios.

The above is the detailed content of 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