How to delete array elements in PHP _ PHP Tutorial

Source: Internet
Author: User
PHP introduces the function used to delete array elements. We often deal with arrays when writing actual code, which is difficult for beginners. Today, we will introduce how to delete arrays in PHP. we often deal with arrays when writing actual code, which is difficult for beginners. Today, we will introduce how to delete array elements in PHP.

It is very easy to add elements to arrays in PHP. just use the value assignment function. The Keys of the arrays are automatically added. but what about deleting the elements in the arrays? Have you ever thought about it? Is it rare? Recently, when I was dealing with a shopping basket program, I encountered the problem of using PHP to delete array elements. after searching for a long time, I finally found the method for deleting arrays, which is actually very simple.
At first, I referred to the method in the article "string array, delete array elements" (in OSO) and used unset, but it has 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 PHP deletes the array elements, the number of elements in the array (obtained by count () has changed, but the array subscript has not been rearranged, you must also use the key before the array to delete 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 to replace array elements. However, if no replacement value is added, the elements are simply deleted. the usage of array_splice () is as follows:
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/PHP deleted from the array. if it is omitted, it starts from offset to the end of the array. it can be positive or negative. The principle is the same as offset. relacement is the value to be replaced.


Bytes. Today, we will introduce how to delete arrays in PHP...

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.