How to delete array elements in PHP _ PHP Tutorial

Source: Internet
Author: User
Explore how to delete array elements in PHP. For the start of learning, I use unset with reference to other methods, but there is a defect. for example, $ a is an array :? $ Aarray (red, green, blue, yellow); count ($ a); get 4 unset ($ a [1]); deleteAt first, I used unset with reference to other methods, but there was a defect. for example, $ a is an array:

 
 
  1. $A=Array("Red", "green", "blue", "yellow ");
  2. Count ($ a); // Get 4
  3. Unset ($ a [1]); // deletes the second element.
  4. Count ($ a); // Get 3
  5. Echo $ a [2]; // The array contains only three elements. In this example, we want to get the last element, but we get blue,
  6. Echo $ a [1]; // no value
  7. ?>

That is to say, after the elements in the array are deleted, the number of elements in the array (obtained using count () has changed, but the array subscript has not been rearranged, you must also use PHP to delete the key before the array element to operate the corresponding value.

Later, I used another method. In fact, it was not called a "method" at all. Instead, I used the PHP4 ready-made function array_splice ().

 
 
  1. $A=Array("Red", "green", "blue", "yellow ");
  2. Count ($ a); // Get 4
  3. Array_splice ($ a,); // deletes the second element.
  4. Count ($ a); // Get 3
  5. Echo $ a [2]; // obtain yellow
  6. Echo $ a [1]; // Get blue
  7. ?>

By comparing this program with the previous one, we can see that array_splice () not only deletes elements, but also rearranges elements, in this way, there will be no null values (such as $ a [1] in values) between the elements of the array.

Array_splice () is actually a function used to replace array elements. However, if no replacement value is added, PHP simply deletes array elements. The following describes the usage of array_splice:

 
 
  1. array array_splice (array input, int offset
    [, int length [, array replacement]])

The input parameter is an array to be operated. the offset parameter starts from the first element and starts from the first element when the offset parameter is negative; length is the number of elements to be replaced/deleted. if it is omitted, it starts from offset to the end of the array. it is also positive and negative. The principle is the same as offset. relacement is the value to be replaced.

The PHP function used to delete array elements is already a standard function of PHP4, but it was not mentioned in the PHP4 manual in my hand. it was found in the latest php.net manual downloaded. I don't know. I was shocked. the PHP4gb in my hand (I believe it is also in most hands) is too old, and many functions do not. you need to know that PHP is famous for its complete functions. if we don't even know many functions, how can we raise the domestic programming level? I really hope a group of PHP enthusiasts can translate the latest PHP Manual again. I am at the "IDE Technology Center"


Starting with begin, I used unset with reference to other methods, but there is a defect. for example, $ a is an array :? $ A = array ("red", "green", "blue", "yellow"); count ($ ); // Get 4 unset ($ a [1]); // delete...

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.