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-05-11 14:25:293712browse

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!

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