How to delete elements in PHP array (unset, array_splice)?
If you want to delete an element in an array, you can use unset directly, but the index of the array will not be rearranged:
<?php
$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
Copy after login
The result is:
Array ( [0] => a [2] => c [3] => d )
Copy after login
So how can the missing elements be filled and the array re-indexed? The answer isarray_splice():
<?php
$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr);
Copy after login
The result is:
Array ( [0] => a [1] => c [2] => d )
Copy after login
Articles you may be interested in
- PHP removes null elements from an array (array_filter)
- php finds whether a value exists in an array (in_array (),array_search(),array_key_exists())
- How to delete the first and last elements of an array in php
- Summary of JavaScript array operation functions (push, pop, join, shift, unshift, slice, splice, concat)
- The difference between PHP merging arrays + and array_merge
- php clears null elements in the array
- PHP array function array_walk() notes
- php gets the last element of the array
http://www.bkjia.com/PHPjc/894202.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/894202.htmlTechArticleHow to delete elements in PHP array (unset, array_splice)? If you want to delete an element in an array, you can use unset directly, but the index of the array will not be rearranged: ?php $arr =...