How to remove an element from an array in php

Guanhui
Release: 2023-03-01 07:28:02
Original
3639 people have browsed it

How to remove an element from an array in php

How to remove an element from an array in php

1. Directly use the "unset()" function to pass in an element in the array "unset()";

$arr = [ 'username' => 'xiaoliang', 'email' => '123456@qq.com' ]; unset($arr['username']);
Copy after login

2. Use the "array_splice()" function to remove an element from the array;

if(($key = array_search('day',$arr))){ array_splice($arr, $key,1); }
Copy after login

3. Use "foreach" to loop to a specific element to "unset" ()" is enough.

foreach($array as $k=>$v){ if($v == 'day'){ unset($array[$k]): } }
Copy after login

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
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!