Php method of removing repeating elements in an array, _php tutorial

Source: Internet
Author: User
Tags learn php programming

PHP removes the method of repeating elements in the array,


Several methods of removing array elements in PHP in many cases our arrays will be duplicated, then we delete some of the duplicates in the array what to do, the elements must keep him unique, so we want to delete them, the following is a traversal query to remove the repeating array elements of several methods.
method One, complete delete duplicate array instance----- to delete an element in an 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 4unset ($a [1]); Delete the second element count ($a); Get 3echo $a [2]; There are only three elements in the array, and I want to get the last element, but get Blue,echo $a [1]; No value?>

That is, after the elements in the array are deleted, the number of elements in the array (with count () is changed, but the array subscript is not rearranged, and the corresponding value must be manipulated with the key before the array is deleted.
Later I used another method, in fact, is not called "method", is to use PHP4 ready-made function Array_splice ().

Count ($a); Get 4array_splice ($a, 1, 1); Delete the second element count ($a); Get 3echo $a [2]; Get Yellowecho $a [1]; Get blue?>

Method Two, delete the function of repeating element 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]);}

Supplementary small Examples:

method One, PHP has built-in functions array_unique can be used to delete duplicate values in an array

    • Array_unique--Removes duplicate values from the divisor group
    • Array_unique description
    • Array array_unique (array array)
    • Array_unique () accepts array as input and returns a new array with no duplicate values

Note The key name remains unchanged. Array_unique () Sorts the values first as a string, then retains only the first key name encountered for each value, and then ignores all subsequent key names. This does not mean that the key name of the first occurrence of the same value in an unordered array is preserved.
Note: When and only if (string) $elem 1 = = = (string) $elem 2 o'clock two units are considered identical. That is, when the expression of a string is the same.
The first unit will be retained.
Example: Array_unique ()

The example above will output:

method Two, Array_flip to achieve de-weight effect

Another method is to use PHP's Array_flip function to indirectly implement the redo effect.
Array_flip is a function that reverses array keys and values, and it has an attribute that if two values in the array are the same, then the last key and value are preserved after the reversal, and we use this feature to indirectly implement the weight of the array.

<?php$arr = Array ("A" = "A1", "B" = "B1", "c" = "A2", "D" = "A1"); $arr 1 = array_flip ($arr);p Rint_r ($arr 1) ;//Invert once, remove duplicate values, output array ([A1] + d [B1] + b [A2] + c) $arr 2 = Array_flip ($arr);p Rint_r ($arr 2);//reverse back, get the array after the deduplication, Output Array ([A1] + d [B1] = b [A2] + c) $arr 3 = Array_unique ($arr);p Rint_r ($arr 3);//Use PHP Array_unique function to go heavy, output arr Ay ([a] = a1 [b] = B1 [c] = A2)?>

Two different methods are obtained by using Array_flip is the last key and value of the repeating element, Array_unique is the first key and value of two repeating elements.

It is hoped that this article will help you learn PHP programming, solve the problem of duplicate elements of the array.

Articles you may be interested in:

    • Php method for sorting two-dimensional arrays by specified key values
    • PHP submit Post Array parameter example analysis
    • PHP Array function array_key_exists () Summary
    • PHP Multidimensional Array Traversal method (2 implementation methods)
    • A summary of some common functions of increment, delete and interpolation of logarithm groups in PHP
    • Explain the definition of PHP arrays and how to create them
    • Random merging of arrays and sorting based on PHP implementation (original sort)
    • A method for iterating through multidimensional arrays in PHP
    • Simple implementation method of PHP multidimensional array to one-dimensional array

http://www.bkjia.com/PHPjc/1084568.html www.bkjia.com true http://www.bkjia.com/PHPjc/1084568.html techarticle PHP Removes the method of repeating elements in the array, several PHP delete array element Methods in many cases, our array will be duplicated, then we delete the array of some duplicate content how ...

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