Summary of the usage of the element functions in the PHP delete array

Source: Internet
Author: User
The deletion of the array elements in PHP is very simple, mainly unset,array_splice, but about the two have, for a long time mixed, sometimes even the difference.

A lot of places are so different unset,array_splice

If the Array_splice () function is deleted, the index value of the array also changes.
If the unset () function is removed, the index value of the array does not change.

In fact, I think there is a precondition for this distinction.

$arr 0=array (' A ', ' B ', ' C ', ' d ', ' f '); $arr 1=array (' name ' = ' Lidequan ', ' tel ' = ' 131****1234 ', ' height ' = ' 175 ', ' Weight ' = ' + '); $arr 2=array (1=> ' Lidequan ',3=> ' 131****1234 ',4=> ') 175 ',6=> '); $arr 3=array (' a ' = > ' Lidequan ',3=> ' 131****1234 ',4=> ' 175 ',10=> ' 1705 ', ' b ' = ' 65 ');//1. Array_splice ($arr 0,2,1); 2. Unset ($arr 0[2]);//3. Array_splice ($arr 1,2,1);//4. unset ($arr 1[' height ');//5. Array_splice ($arr 2,2,1);//6. Unset ($arr 1[4]);//7.array_splice ($arr 3,2,1);

1 and 2 do different results

1 Execution Results:

Array (size=4)  0 = String ' a ' (length=1)  1 = = String ' B ' (length=1)  2 = String ' d ' (length=1)  3 = = String ' F ' (length=1)

2 Execution Results:

Array (size=4)  0 = String ' a ' (length=1)  1 = = String ' B ' (length=1)  3 = String ' d ' (length=1)  4 = = String ' F ' (length=1)

The results of the 3 and 4 executions are the same.

Array (size=3)  ' name ' = = String ' Lidequan ' (length=8)  ' tel ' = = String ' 131****1234 ' (length=11)  ' Weight ' = ' + String ' (length=2)

5 and 6 of the execution results are not kind of

5 The results of the implementation are as follows

Array (size=3)  0 = String ' Lidequan ' (length=8)  1 = String ' 131****1234 ' (length=11)  2 = String ' 6 5 ' (length=2)

6 The results of the implementation are as follows:

Array (size=3)  1 = String ' Lidequan ' (length=8)  3 = String ' 131****1234 ' (length=11)  6 = String ' 6 5 ' (length=2)

The results of the 7 implementation are as follows:

Array (size=4)  ' a ' + = String ' Lidequan ' (length=8)  0 = String ' 131****1234 ' (length=11)  1 = string ' 1705 ' (length=4)  ' b ' = + string ' (length=2)

Summary: This shows that, Unset,array_splice, in the deletion of array elements, the array index values vary in the principle that

The unset function is to delete the elements in the array by the array key values, and after deletion, no operations are performed on the arrays.

The Array_splice function deletes elements based on the order of the elements in the array (the order starts from 0), deletes the element, deletes the element, and, if there are elements of an integer key value in the array, the elements are updated sequentially from 0~n according to their order size in the array.

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.