I wrote an article about array deduplication before, but it was limited to one-dimensional arrays. The following function can be used for two-dimensional arrays:
Copy code The code is as follows:
//Remove duplicate values from the two-dimensional array
function array_unique_fb($array2D)
{
foreach ($array2D as $v)
{
$v = join(",",$v); //Dimensionality reduction, also You can use implode to convert a one-dimensional array into a string connected with commas
$temp[] = $v;
}
$temp = array_unique($temp); //Remove duplicate strings , that is, a repeated one-dimensional array
foreach ($temp as $k => $v)
{
$temp[$k] = explode(",",$v); // Then reassemble the disassembled array
}
return $temp;
}
If you want to retain the key values of the array, you can use the following function:
Copy code The code is as follows:
//The two-dimensional array removes duplicate values and retains key values
function array_unique_fb($array2D)
{
foreach ($array2D as $k=>$v)
{
$v = join(",",$v); //For dimensionality reduction, implode can also be used, Convert a one-dimensional array to a comma-connected string
$temp[$k] = $v;
}
$temp = array_unique($temp); //Remove duplicate strings, also It is a repeated one-dimensional array
foreach ($temp as $k => $v)
{
$array=explode(",",$v); //Then split the array Reassemble
$temp2[$k]["id"] =$array[0];
$temp2[$k]["litpic"] =$array[1];
$temp2[ $k]["title"] =$array[2];
$temp2[$k]["address"] =$array[3];
$temp2[$k]["starttime"] =$array[4];
$temp2[$k]["endtime"] =$array[5];
$temp2[$k]["classid"] =$array[6];
$temp2[$k]["ename"] =$array[7];
}
return $temp2;
}
That's it.
Two-dimensional array deduplication
Copy code The code is 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);
?>
http://www.bkjia.com/PHPjc/323968.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323968.htmlTechArticleI wrote an article about array deduplication before, but it was limited to one-dimensional arrays. The following function can be used for two-dimensional arrays: Copy the code The code is as follows: //Remove duplicate values from the two-dimensional array fun...