Home  >  Article  >  Backend Development  >  PHP method to delete specific array contents and rebuild array index._PHP Tutorial

PHP method to delete specific array contents and rebuild array index._PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:20807browse

Copy code The code is as follows:

$a = array('a','b','c','d') ;
unset($a[2]);
print_r($a);

But the biggest disadvantage of this method is that the array index is not rebuilt.

After checking the information, it turns out that PHP provides this function, but it is very indirect.

This function is array_splice.

For the convenience of use, I encapsulated it into a function for everyone to use.
Copy code The code is as follows:

function array_remove(&$arr,$offset){
array_splice($arr ,$offset,1);
}
$a = array('a','b','c','d');
array_remove($a,2);
print_r($a);

After testing, we can know that the element at position 2 is actually deleted and the index is re-established.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323003.htmlTechArticleCopy the code The code is as follows: $a = array('a','b','c','d '); unset($a[2]); print_r($a); But the biggest disadvantage of this method is that it does not rebuild the array index. After checking the information, it turns out that PHP provides...
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