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.