Delete array element _ PHP Tutorial

Source: Internet
Author: User
Delete array elements. 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, it is very easy to add elements to an array in PHP. just assign values directly. The Keys of the array will be automatically added, but what about deleting the elements in the array? Have you ever thought about it? Is it rare? Recently, when I was dealing with a shopping basket program, I encountered the problem of deleting elements in the array. after searching for a long time, I finally found the method to delete the array, 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:
Count ($ a); // Get 4
Unset ($ a [1]); // deletes the second element.
Count ($ a); // Get 3
Echo $ a [2]; // The array contains only three elements. In this example, we want to get the last element, but we get blue,
Echo $ a [1]; // no value
?>
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 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 ().
Count ($ a); // Get 4
Array_splice ($ a,); // deletes the second element.
Count ($ a); // Get 3
Echo $ a [2]; // obtain yellow
Echo $ a [1]; // Get blue
?>
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/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.
This function is already a standard function of PHP4, but I did not mention it in the PHP4 manual in my hand. it was found in the latest php.net manual I 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 hope a group of PHP enthusiasts can translate the latest PHP Manual again. I saw the webmaster have a "classic PHP Manual" plan at the "Eide Technology Center" (http://tech.addcn.com/), but it seems that there are not many applicants. if you are interested, you can join it.

Why? Have you ever thought about it? Is it rare? I am...

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.