PHP array de-weight (one-dimensional and two-dimensional arrays)

Source: Internet
Author: User
Tags pear

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))

Reprinted from: http://www.cnblogs.com/srsee/archive/2013/05/09/srsee.html

PHP array de-weight (one-dimensional and two-dimensional arrays)

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.