PHP functions for deleting array elements and repeated arrays

Source: Internet
Author: User
Tags arrays
The code is as follows: Copy code

$ A = array ("red", "green", "blue", "yellow ");
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

// Array array_splice (array input, int offset [, int length [, array replacement])
// 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:
$ B = array ("red", "green", "blue", "yellow ");
Array_splice ($ a, 1, 1 );

  


// The following describes how to comprehensively delete duplicate values and delete specified array elements.

The code is as follows: Copy code

$ Array1 = array (1 => "www.111cn.net", 2 => "pineapple", 4 => "www.111cn.net", 3 => "banana", 4 => "Ba Le ", 5 => "www.111cn.net", 6 => "www.111cn.net ");

$ Search_keys = array_keys ($ array1, "www.111cn.net ");

Foreach ($ search_keys as $ key ){
Unset ($ array1 [$ key]);
}


Print_r ($ array1 );

/*
Expected result
Array ([2] => pineapple [4] => Ba Le [3] => bananas)
*/

The code is as follows: Copy code
// Delete the function of repeating elements in the 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]);
}


?>

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.