Example of deleting elements from php array_PHP tutorial

WBOY
Release: 2016-07-13 10:35:27
Original
709 people have browsed it

Let’s give an example:

Copy code The code is as follows:

$arr = array('a','b','c','d');
unset($arr[1]);
print_r($arr);
?>

I previously imagined that after unset, the array $arr should compress the array to fill in the missing element positions, but after print_r($arr), the result is not like that. The final result is Array ( [0] => a [2] => c [3] => d );
If this is the case, then let’s take a look at the form of a digital array

Copy code The code is as follows:

$arr = range(5,10,4) ;
print_r($arr);//Array ( [0] => 5 [1] => 6 [ 2] => 7 [3] => 8 [4] => 9 [5] => 10 )
unset($arr[1]);//Array ( [0] => 5 [2] => 7 [3] => 8 [4] => 9 [5 ] => 10 )
print_r($arr);
?>

You can see that the output is in the form of an array, which will fill in the missing elements. Location. So how can we ensure that missing elements are filled in and the array is re-indexed? The answer is array_splice():
Copy code The code is as follows:

$arr = array('a','b','c','d');
array_splice($arr,1,1);
print_r($arr) ; // Array ( [0] => a [1] => c [2] => d )< /span>
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/744325.htmlTechArticleLet’s give an example: Copy the code as follows: ?php $arr = array('a','b ','c','d'); unset($arr[1]); print_r($arr); ? What I imagined before was that after unset, the array $arr should be compressed...
Related labels:
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