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

How to remove an element from an array in PHP?

Guanhui
GuanhuiOriginal
2020-06-10 17:40:262887browse

How to remove an element from an array in PHP?

#PHP How to remove an element from an array?

In PHP, you can use the "array_shift()" function to remove an element from the array. This function will move the unit at the beginning of the array out of the array. Its syntax is "array_shift(array)", and its parameter array Indicates the array to be operated on, and the return value is the removed value. This function will directly operate on the array.

array_shift() Shifts the first element of array out and returns it as the result, decrements the length of array by one and shifts all other elements forward one position. All numeric key names will be changed to count from zero, and text key names will remain unchanged.

Code Example

<?php
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_shift($stack);
print_r($stack);
?>

The above routine will output:

Array
(
    [0] => banana
    [1] => apple
    [2] => raspberry
)

And orange is assigned to $fruit.

Recommended tutorial: "PHP Tutorial"

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