PHP functions for deleting array elements and repeated Arrays

Source: Internet
Author: User

PHP Delete array elements and delete repeated array functions this article mainly describes how to delete php array values and how to delete elements at the specified position of an array, the second section tells you how to use the array_keys letter.

Php tutorial Delete array elements and delete repeated array Functions
This article mainly talks about the deletion of php array values. It tells you how to delete an element at the specified position of an array. The second section tells you how to use the array_keys function to delete repeated elements of an array.
*/

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

$ Array1 = array (1 => "www.bkjia.com", 2 => "pineapple", 4 => "=>" banana ", 4 =>" ba le ", 5 => "=>" www.bkjia.com ");

$ Search_keys = array_keys ($ array1, "www.bkjia.com ");

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


Print_r ($ array1 );

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

// 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.