Php deletes repeated elements in an array. _ PHP Tutorial

Source: Internet
Author: User
Php deletes repeated elements in an array ,. Php deletes repeated elements in an array. in many cases, several php deletes array elements, how can we delete some repeated elements in the array in php,

Several php methods to delete array elements. in many cases, our array will be duplicated. 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. below we use traversal queries to delete repeated array elements.
Method 1. completely delete the duplicate array instance -----Deletes an element from 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 4 unset ($ a [1]); // delete the second element count ($ ); // 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", but it was made out of php4.Function array_splice ().

Count ($ a); // get 4array_splice ($ a,); // delete the second element count ($ a); // Get 3 echo $ a [2]; // Obtain yellowecho $ a [1]; // Get blue?>

Method 2: delete the function of repeated 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]);}


Method 1,Php has a built-in function, array_unique, which can be used to delete repeated values in the array.

  • Array_unique -- Remove repeated values from the array
  • Array_unique description
  • Array array_unique (array)
  • Array_unique () accepts array as input and returns a new array without repeated values.

Note that the key name remains unchanged. Array_unique () first sorts the values as strings, then retains only the first key name that is encountered for each value, and then ignores all the key names that follow. This does not mean that the first key name that appears for the same value in the unordered array will be retained.
Note: when and only when (string) $ elem1 = (string) $ elem2, the two units are considered the same. That is, when the expression of a string is the same.
The first unit will be retained.
Example: array_unique ()

<?php$input = array("a" => "green", "red", "b" => "green", "blue", "red");$result = array_unique($input);print_r($result);?> 

The above example will output:

Array( [a] => green [0] => red [1] => blue) 

Method 2,Array_flip achieves deduplication

Another method is to use the array_flip function of php to indirectly implement deduplication.
Array_flip is a function used to reverse the array key and value. it has a feature that if two values in the array are the same, the last key and value will be retained after the inversion, we use this feature to indirectly implement array deduplication.

<? Php $ arr = array ("a" => "a1", "B" => 'b1 ', "c" => "a2 ", "d" => "a1"); $ arr1 = array_flip ($ arr); print_r ($ arr1); // first reverse the string to remove the duplicate value, output Array ([a1] => d [b1] => B [a2] => c) $ arr2 = array_flip ($ arr); print_r ($ arr2 ); // reverse it back to get the de-duplicated Array and output Array ([a1] => d [b1] => B [a2] => c) $ arr3 = array_unique ($ arr); print_r ($ arr3); // Remove duplicates using the array_unique function of php, output Array ([a] => a1 [B] => b1 [c] => a2)?>

The two methods are different:Array_flipThe final key and value of the repeating element are obtained.Array_uniqueThe first key and value of two duplicate elements are obtained.

I hope this article will help you learn php program design and solve the problem of repeated array elements.

Articles you may be interested in:
  • PHP sorts two-dimensional arrays by specified key values
  • Php submits post array parameter instance analysis
  • Summary of the php array function array_key_exists ()
  • PHP multi-dimensional array traversal method (two implementation methods)
  • Summary of some common addition, deletion, and insertion functions of arrays in PHP
  • Explanation of PHP's definition of arrays and how to create arrays
  • Php-based random array merging and sorting (original sorting)
  • Php implements multi-dimensional array traversal
  • Simple PHP multi-dimensional array to one-dimensional array implementation method

Methods, several php methods to delete array elements. in many cases, our array will be duplicated. how can we delete some repeated content in the array...

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