Home > Article > Backend Development > 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']);
2. Use the "array_splice()" function to remove an element from the array;
if(($key = array_search('day',$arr))){ array_splice($arr, $key,1); }
3. Use "foreach" to loop to a specific element to "unset" ()" is enough.
foreach($array as $k=>$v){ if($v == 'day'){ unset($array[$k]): } }
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!