首页 > 代码库 > PHP如何去掉多维数组的重复值
PHP如何去掉多维数组的重复值
1.定义函数
function array_unique_new($arr){
$t = array_map(‘serialize‘, $arr);//利用serialize()方法将数组转换为以字符串形式的一维数组
$t = array_unique($t);//去掉重复值
$new_arr = array_map(‘unserialize‘, $t);//然后将刚组建的一维数组转回为php值
return $new_arr;
}
2.定义数组
$arr = array(
array(
‘sup_name‘ => ‘上海‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯1‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海2‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯2‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海4‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯1‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯3‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海1‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯4‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯3‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
array(
‘sup_name‘ => ‘上海1‘,
‘cn_name‘ => ‘EthylAcetate‘,
‘en_name‘ => ‘乙酸乙酯4‘,
‘pinyin‘ => ‘yisuanyizhi‘,
‘pro_info‘ => ‘耗材‘,
),
);
3.执行测试
$new_arr = $this->array_unique_new($arr);
echo ‘<pre/>‘;
print_r($new_arr);
PHP如何去掉多维数组的重复值