Home  >  Article  >  Backend Development  >  How to delete a value from an array in php

How to delete a value from an array in php

PHPz
PHPzOriginal
2023-04-25 09:05:50553browse

Array is one of the very important data types in PHP, which can be used to store multiple values. When we need to delete a value in an array, PHP provides multiple implementation methods. This article will introduce these implementation methods and their advantages and disadvantages.

  1. unset function

The unset function is a commonly used function in PHP to delete variables. It can also be used to delete an element in an array. It is very simple to use the unset function to delete elements in an array. You only need to specify the key name of the element to be deleted. For example:

$array = array(1, 2, 3, 4, 5);
unset($array[2]);      // 删除键名为2的元素
print_r($array);

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

Array
(
    [0] => 1
    [1] => 2
    [3] => 4
    [4] => 5
)

Okay As you can see, the element with key name 2 has been deleted.

The advantage of using the unset function to delete array elements is that it is simple and convenient. The disadvantage is that it may cause the array to be re-indexed. If multiple elements are deleted, a lot of space will be wasted. In addition, if you are not sure whether the element to be deleted exists, using the unset function will report an error.

  1. array_splice function

array_splice function is a powerful array processing function in PHP, which can delete elements in the array and return the deleted elements. To use the array_splice function to delete an element in an array, you need to specify the key name of the element to be deleted and the number of elements to be deleted, for example:

$array = array(1, 2, 3, 4, 5);
array_splice($array, 2, 1);     // 删除键名为2的元素
print_r($array);

Run the above code, the output result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)

You can see that the element with key name 2 has been deleted.

The advantage of using the array_splice function to delete array elements is that it can return the deleted elements. The disadvantage is that it is slightly more cumbersome to use than the unset function. In addition, if you are not sure whether the element to be deleted exists, using the array_splice function will not report an error, but it will not perform any operation.

  1. array_diff function

The array_diff function is a commonly used array processing function in PHP. It is used to calculate the difference set of arrays. To use the array_diff function to delete an element in an array, you need to first use the array_diff function to pass in the element to be deleted, and then use the array_values ​​function to re-index the array, for example:

$array = array(1, 2, 3, 4, 5);
$delete_value = 3;      // 要删除的元素
$array = array_values(array_diff($array, array($delete_value)));
print_r($array);

Run the above code, the output result is as follows:

Array
(
    [0] => 1
    [1] => 2
    [2] => 4
    [3] => 5
)

You can see that the element with value 3 has been deleted.

The advantage of using the array_diff function to delete array elements is that it is simple and does not waste space. The disadvantage is that you need to pay attention to whether the value passed in is a subset of the array. In addition, if you are not sure whether the element to be deleted exists, using the array_diff function will not report an error.

To sum up, you can use the unset function, array_splice function, and array_diff function to delete an element in the array. They each have their own advantages and disadvantages, and you need to choose the appropriate implementation method according to the specific situation.

The above is the detailed content of How to delete a value from an 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