Home > Backend Development > PHP Tutorial > Comparison of methods for deleting specified values ​​in an array in php_PHP Tutorial

Comparison of methods for deleting specified values ​​in an array in php_PHP Tutorial

WBOY
Release: 2016-07-13 16:59:20
Original
829 people have browsed it

You also have a function yourself. PHP tutorial. Many built-in functions have problems (not errors, but narrow applicability)

for(old array...){
if(is the value to be deleted)
Continue
$newArr[]=each value
}

return $newArr

Example

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]):
}
}

The efficiency of using foreach is not high, see PHP’s built-in functions

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

Example 1

$arr = array_flip($arr);

unset($arr['world']);

$arr = array_flip($arr);

print_r($arr);

Example 2

The array_search() function is the same as in_array(), searching for a key value in the array. If the value is found, the key of the matching element is returned. If not found, returns 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]);

}


Example 3

The array_splice() function is similar to the array_slice() function, selecting a range of elements in an array, but instead of returning them, it deletes them and replaces them with other values.

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

array_splice($arr, $key,1);

}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631330.htmlTechArticleYou have a function yourself. Many functions built into the PHP tutorial have problems (not errors, but narrow applicability) for (old array....){ if (is the value to be deleted) continue $newArr[]=each value}...
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