PHP array De-weight

Source: Internet
Author: User
Tags pear

PHP array De-weight(2011-04-13 18:03:53) reproduced
tags: Gossip Category: JavaScript
The removal of duplicates in an array 2010-07-28 15:29

duplicate entries for one-dimensional arrays :

Using the Array_unique function, you can use the following example:

<?php
$aa =array ("apple", "banana", "pear", "apple", "wail", "watermalon");
$BB =array_unique ($aa);
Print_r ($bb);
?>

The results are as follows: Array ([0] = Apple [1] = Banana [2] = Pear [4] = Wail [5] = watermalon).

duplicate entries for two-dimensional arrays :

For a two-dimensional array we can be discussed in one way or another because the value of one key name cannot be duplicated, and the other is because the inner one-dimensional array cannot be identical and the duplicates are deleted, the following example Illustrates:

㈠ Delete duplicates because the value of a key name cannot be duplicated

<?php
function Assoc_unique ($arr, $key)
{
$tmp _arr = Array ();
foreach ($arr as $k = $v)
{
If (in_array ($v [$key], $tmp _arr))//search $v[$key] is present in the $tmp_arr array, if there is a return true
{
unset ($arr [$k]);
}
else {
$tmp _arr[] = $v [$key];
}
}
Sort ($arr); Sort function sorting an array
Return $arr;
}

$AA = Array (
Array (' id ' = = 123, ' name ' = ' Zhang San '),
Array (' id ' = = 123, ' name ' = ' John Doe '),
Array (' id ' = = 124, ' name ' = ' Harry '),
Array (' id ' = ' = ', ' name ' = ' Zhao Liu '),
Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
);
$key = ' ID ';
Assoc_unique (& $aa, $key);
Print_r ($aa);
?>

The result is: array ([0] = = Array ([id] = = 123 [name] = + Zhang San) [1] = = Array ([id] = = 124 [name] = Harry) [2] =& Gt Array ([id] = [name] = + Zhao Liu) [3] = = Array ([id] = 126 [name] = Zhao Liu))

㈡ because an internal one-dimensional array cannot be exactly the same, and delete duplicates

<?php
function Array_unique_fb ($array 2D) {
foreach ($array 2D as $v) {
$v = Join (",", $v); dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
$temp [] = $v;
}
$temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
foreach ($temp as $k = = $v) {
$temp [$k] = Explode (",", $v); Re-assemble the disassembled array
}
Return $temp;
}

$AA = Array (
Array (' id ' = = 123, ' name ' = ' Zhang San '),
Array (' id ' = = 123, ' name ' = ' John Doe '),
Array (' id ' = = 124, ' name ' = ' Harry '),
Array (' id ' = = 123, ' name ' = ' John Doe '),
Array (' id ' = = 126, ' name ' = ' Zhao Liu ')
);
$BB =ARRAY_UNIQUE_FB ($aa);
Print_r ($bb)
?>

Display Results: Array ([0] = = Array ([0] = = 123 [1] = Zhang San) [1] = = Array ([0] = = 123 [1]/= Li four) [2] + = array ([0] = 124 [1] = Harry) [4] = = Array ([0] = 126 [1] = Zhao Liu))

PHP array De-weight

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.