PHP delete array elements and delete duplicate array function

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags array array_keys array_splice delete function get how to location

php tutorial delete array elements and delete duplicate array function This article is mainly about the deletion of the php array value Oh, tell you how to delete an array of elements at the specified location, the two tell you to use the array_keys function to delete the array of duplicate elements.
* /

$ a = array ("red", "green", "blue", "yellow");
count ($ a); // get 4
unset ($ a [1]); // Delete the second element
count ($ a); // get 3
Echo $ a [2]; // array only three elements, wanted to get the last element, but got blue,
Echo $ a [1]; / / No value

// array array_splice (array input, int offset [, int length [, array replacement]])
// array_splice () is actually a function that replaces an array element, but simply deletes the element without substitution. Here's how to use array_splice ():
$ b = array ("red", "green", "blue", "yellow");
array_splice ($ a, 1,1);


// See below a more comprehensive delete duplicate values ​​and delete the specified array element

$ array1 = array (1 => "www.jzread.com", 2 => "Pineapple", 4 => "www.jzread.com", 3 => "Banana", 4 => "Guava", 5 => "www.jzread.com", 6 => "www.jzread.com");

$ search_keys = array_keys ($ array1, "www.jzread.com");

foreach ($ search_keys as $ key) {
unset ($ array1 [$ key]);
}

print_r ($ array1);

/ *
got the answer
array ([2] => pineapple [4] => guava [3] => banana)
* /

// 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]);
}
?>

Related Article

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.