Home > Backend Development > PHP Problem > How to remove an element from php array

How to remove an element from php array

PHPz
Release: 2023-04-23 19:38:39
Original
788 people have browsed it

In PHP, array is a very commonly used data structure. When writing PHP programs, we often need to operate on arrays, such as adding elements to the array, modifying the value of the elements, or removing elements from the array.

This article will introduce how to remove an element in PHP.

In PHP, there are two methods to remove elements from an array: unset and array_splice.

Method 1: Use the unset() function

The unset() function can be used to remove one or more elements from an array. Its syntax is as follows:

unset(array $array[, mixed $...])
Copy after login

Among them, $array is the array to be operated on, $... is the key name of the element to be removed, one or more can be specified.

Sample code:

<?php
$array = array(1, 2, 3, 4, 5);
unset($array[1]); // 移除第二个元素
print_r($array);
?>
Copy after login

Output result:

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

In the above code, we use the unset() function to remove the second element in the array, that is Element with key name 1.

It should be noted that when using the unset() function to remove array elements, the keys of the array will not be reordered. So, in the example code above, although we removed the second element, the array still contains five elements, just with a gap in the middle.

Method 2: Use array_splice() function

array_splice() function can be used to remove specified elements in the array and retain the key names in the array. Its syntax is as follows:

array_splice(array &$input, int $offset[, int $length[, mixed $replacement]])
Copy after login

Among them, $input is the array to be operated on, $offset is the position of the element to be removed, $length is the number of elements to be removed, and $replacement is used to replace the moved element. An array of elements to be divided. If not specified, it means no replacement will be performed.

Sample code:

<?php
$array = array(1, 2, 3, 4, 5);
array_splice($array, 1, 1); // 移除第二个元素
print_r($array);
?>
Copy after login

Output result:

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

In the above code, we use the array_splice() function to remove the second element in the array, that is The element at position 1.

It should be noted that when using the array_splice() function to remove elements, the keys of the array will be reordered. So, in the example code above, after we removed the second element, the array only contains four elements, and the keys have been reordered. If you want to keep the key names, you need to set the $replacement parameter to an empty array.

Summary

The above are two ways to remove an element in PHP. It should be noted that using the unset() function to directly remove elements will cause the array key names to change, while using the array_splice() function can preserve the array key names. Therefore, in actual development, different methods need to be selected according to specific circumstances.

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