Home >php教程 >php手册 >php删除数组中指定值方法比较

php删除数组中指定值方法比较

WBOY
WBOYOriginal
2016-06-13 10:03:501049browse

你自己也一个 函数呀 php教程 内置的很多函数都有问题(不是错误,是适用方位窄)

for(旧数组.....){
  if(是要删除的值)
    continue
  $newArr[]=每个值
}

return $newArr

实例

Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.)

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

利用foreach效率不高下在, 看php自带函数

$arr = array("Hello","world","It's","beautiful","day");

实例一

$arr = array_flip($arr);

unset($arr['world']);

$arr = array_flip($arr);

print_r($arr);

实例二

array_search() 函数与 in_array() 一样,在数组中查找一个键值。如果找到了该值,匹配元素的键名会被返回。如果没找到,则返回 false。

 

$arr = Array([0] => Hello[1] => world.[2] => It's[3] => a[4] => beautiful[5] => day.);

if(($key = array_search('day',$arr))){

    unset($arr[$key]);

}


实例三

array_splice() 函数与 array_slice() 函数类似,选择数组中的一系列元素,但不返回,而是删除它们并用其它值代替。

 

if(($key = array_search('day',$arr))){

    array_splice($arr, $key,1);

}

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