Explore how to implement PHP to delete array elements _php tutorial

Source: Internet
Author: User
For learningI began by referring to other methods, using unset, but with a flaw. A $ A is an array:

 
  
  
  1. $ a = Array ("Red", "green", "blue", "yellow");
  2. Count ($a); Get 4
  3. unset ($a [1]); Delete the second element
  4. Count ($a); Get 3
  5. echo $a [2]; There are only three elements in the array, and I wanted to get the last element, but I got blue,
  6. echo $a [1]; No value
  7. ?>

That is, after the elements in the array are deleted, the number of elements in the array (with count () is changed, but the array subscript is not rearranged, and the corresponding value must be manipulated by removing the key from the array element before PHP.

Later I used another method, in fact, is not called "method", is to use PHP4 ready-made function Array_splice ().

 
  
  
  1. !--? $ a = array ("Red", "green", "blue", "yellow");
  2. count ($a);//Get 4
  3. array_splice ($a, 1, 1);//delete second element
  4. count ($a);//Get 3
  5. echo $a [2];//Get yellow
  6. echo $a [1];//Get blue
  7. ;

Comparing this program to the previous one, you can see that array_splice () not only removes the element, but also rearrange the elements so that there will be no null values in the middle of each element of the array (as in the preceding example, $a[1]).

Array_splice () is actually a function of replacing an array element, but simply PHP removes the array element without replacing the value. The following is the use of Array_splice ():

 
  
  

The parameter input is an array to manipulate; offset is the beginning of the first element, the number of seconds from the beginning, the number from the last element, and the length of the number of elements to be replaced/removed, and when omitted, from offset to the end of the array, can be positively negative, The principle is the same as offset; relacement is the value to be replaced.

This php delete array element function is already the standard function of PHP4, but in my hands of the PHP4 manual did not mention, I was downloaded php.net the latest manual found. I don't know, a look scare, I hand of the PHP4GB (believe is also the hands of most people) It's too old to be a lot of functions. To know that PHP is well-known as a function, if we do not know a lot of functions, and how to put the domestic programming level up? I wish I could have a bunch of PHP keen who can translate the latest PHP manuals again. I'm at the ADE Technology Center.


http://www.bkjia.com/phpjc/446338. HTML www.bkjia.com true http://www.bkjia.com/phpjc/446338.html techarticle for learning to start I refer to other methods, using unset, but there is a flaw. $a is an array:? $ A = Array ("Red", "green", "blue", "yellow"); Count ($a);//Get 4 unset ($a [1]);//delete section ...

  • 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.