Home  >  Article  >  Backend Development  >  How to remove an element from an array in PHP

How to remove an element from an array in PHP

PHPz
PHPzOriginal
2023-04-12 09:19:27409browse

In PHP, array is an important data type. Sometimes, we need to delete an element from an array. In this article, we will discuss about how to delete an element from an array in PHP.

Methods to delete array elements

In PHP, there are two ways to delete elements from an array: using the unset() function and using the array_splice() function.

  1. Use the unset() function

The unset() function is used to destroy the specified variable. When used on an array, it can delete an element from the array. You can use the keys of an array as a parameter, or you can use an array of keys.

The following is the basic syntax for using the unset() function to delete elements from an array:

unset($array[key]);

Where, $array is the array variable of the element to be deleted, and key is the key to be deleted.

The following is an example:

// 创建一个有3个元素的数组
$array = array("one", "two", "three");

// 删除第二个元素
unset($array[1]);

// 打印剩余的元素
print_r($array);

Output:

Array
(
    [0] => one
    [2] => three
)

As shown in the above example, after using the unset() function to delete the element $array[1] in the array, only That leaves $array[0] and $array[2].

It should be noted that the unset() function does not rebuild the array, nor does it reorder the index of the array. That is, after deleting an element from the array via unset(), the keys of the remaining elements will not be automatically renumbered.

  1. Use array_splice() function

array_splice() function can be used to delete a specified range of elements from an array, and can select new elements to replace the deleted elements . The following is the basic syntax of the array_splice() function to delete array elements:

array_splice($array, $offset, $length);

Among them, $array is the array variable of the element to be deleted, $offset is the position of the element to be deleted, and $length is the number of elements to be deleted.

The following is an example:

// 创建一个有3个元素的数组
$array = array("one", "two", "three");

// 删除第二个元素
array_splice($array, 1, 1);

// 打印剩余的元素
print_r($array);

Output:

Array
(
    [0] => one
    [1] => three
)

As shown in the above example, after using the array_splice() function to delete the element $array[1] in the array, only That leaves $array[0] and $array[2].

It should be noted that unlike the unset() function, the array_splice() function will reorder the index of the array. That is, after $array[1] is deleted, the key of $array[2] becomes 1.

Summary

In PHP, you can use the unset() function and array_splice() function to remove elements from an array. When you delete an element using the unset() function, the keys of the remaining elements are not automatically renumbered; when you delete an element using the array_splice() function, the keys of the remaining elements are renumbered.

The above is the detailed content of How to remove an element 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