Analysis of the de-weight problem of the array php two-dimensional array

Source: Internet
Author: User
Tags explode rar
Previously wrote an article about the weight of an array, but only one-dimensional arrays. The following functions can be used with two-dimensional arrays:

Copy the Code code as follows:


Two-dimensional arrays eliminate duplicate values
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;
}


If you want to preserve the key values of the array, you can use the following function:

Copy the Code code as follows:


Two-dimensional array removes duplicate values and preserves key values
function ARRAY_UNIQUE_FB ($array 2D)
{
foreach ($array 2D as $k = $v)
{
$v = Join (",", $v); Dimension, you can also convert one-dimensional array to a comma-concatenated string using implode
$temp [$k] = $v;
}
$temp = Array_unique ($temp); Remove duplicate strings, that is, duplicate one-dimensional arrays
foreach ($temp as $k = $v)
{
$array =explode (",", $v); Re-assemble the disassembled array
$temp 2[$k ["id"] = $array [0];
$temp 2[$k] ["litpic"] = $array [1];
$temp 2[$k ["title"] = $array [2];
$temp 2[$k] [address] = $array [3];
$temp 2[$k] ["starttime"] = $array [4];
$temp 2[$k] ["endtime"] = $array [5];
$temp 2[$k] ["classid"] = $array [6];
$temp 2[$k] ["ename"] = $array [7];
}
return $temp 2;
}


That's about it.
Two-dimensional array de-weight

Copy the Code code as follows:


$arr = Array (
Array (' id ' = = 1, ' name ' = ' aaa '),
Array (' id ' = = 2, ' name ' = ' BBB '),
Array (' id ' = = 3, ' name ' = ' CCC '),
Array (' id ' = = 4, ' name ' = ' ddd '),
Array (' id ' = = 5, ' name ' = ' CCC '),
Array (' id ' = = 6, ' name ' = ' aaa '),
Array (' id ' = = 7, ' name ' = ' BBB '),
);
Function Assoc_unique (& $arr, $key)
{
$rAr =array ();
for ($i =0; $i
{
if (!isset ($rAr [$arr [$i] [$key]])
{
$rAr [$arr [$i] [$key]]= $arr [$i];
}
}
$arr =array_values ($RAR);
}
Assoc_unique (& $arr, ' name ');
Print_r ($arr);
?>

The above is introduced in the array php two-dimensional array of the de-weight problem analysis, including the contents of the array, I hope that the PHP tutorial interested in a friend helpful.

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