PHP array Delete element example _php tutorial

Source: Internet
Author: User
Let's give an example:
Copy CodeThe code is as follows:
$arr = Array (' A ', ' B ', ' C ', ' d ');
Unset ($arr [1]);
Print_r ($arr);
?>

I had imagined that after unset, the array $arr should compress the array to fill the missing element position, but after Print_r ($arr), the result was not that, and the end result was an array ([0] = a [2] = c [3] + D);
If that's the case, let's take a look at the form of a digital array

copy code code is as follows:
!--? php $arr = Range (5,10,4);
Print_r ($arr);// array ([0] = 5 [1] = 6 [2] = 7 [3] =&G T 8 [4] = 9 [5] = +)
unset ($arr [1]);// array ([0] = > 5 [2] = 7 [3] = 8 [4] = 9 [5] = +)
Print_r ($arr);
?

You can see that the output is also in the form of an array where the missing elements are filled. So how can the missing elements be filled and the array will be re-indexed? The answer is Array_splice ():

!--? php 
   
     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)
?

http://www.bkjia.com/phpjc/744325. HTML www.bkjia.com true http://www.bkjia.com/phpjc/744325.html techarticle Let's take an example: Copy the code as follows: PHP $arr = Array (' A ', ' B ', ' C ', ' d '); Unset ($arr [1]); Print_r ($arr);? I previously imagined that after unset, the array $arr should be compressed ...

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.