Several php methods for deleting array elements _ PHP Tutorial-php Tutorial

Source: Internet
Author: User
Several php methods for deleting array elements. Several php methods are used to delete array elements. in many cases, the array will be duplicated. what should we do if we delete some repeated content in the array, I must keep these elements the only several php methods for deleting array elements. in many cases, our array will be duplicated. what should we do if we delete some repeated content in the array, I have to keep these elements unique, so I want to delete them. below are several ways to delete repeated array elements by traversing queries.

Several php tutorials to delete array elements
In many cases, our array will repeat, so what should we do if we delete some repeated content in the array? these elements must be unique, so we can try to delete them, the following describes several methods to delete repeated array elements through traversal queries.


Let's take a look at the complete deletion of duplicate array instances.


// Delete an element from the array
Function array_remove_value (& $ arr, $ var ){
Foreach ($ arr as $ key => $ value ){
If (is_array ($ value )){
Array_remove_value ($ arr [$ key], $ var );
} Else {
$ Value = trim ($ value );
If ($ value = $ var ){
Unset ($ arr [$ key]);
} Else {
$ Arr [$ key] = $ value;
}
}
}
}


$ 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
?>


Method 2

Delete the function of repeating elements in an array
Function delmember (& $ array, $ id)
{
$ Size = count ($ array );
For ($ I = 0; $ I <$ size-$ id-1; $ I ++)
{
$ Array [$ id + $ I] = $ array [$ id + $ I + 1];
}
Unset ($ array [$ size-1]);
}


For more details, see: http://www.bkjia.com/phper/php-function/34794.htm


The distinct method for deleting array elements in many cases will repeat our Array. what should we do if we delete some repeated content in the array? these elements must be unique...

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.