How to remove an element from an array in PHP?

Guanhui
Release: 2023-03-01 14:28:02
Original
2878 people have browsed it

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);
?>
Copy after login

The above routine will output:

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

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!

Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!