码迷,mamicode.com
首页 > 编程语言 > 详细

PHP如何去掉多维数组的重复值

时间:2014-11-21 18:07:35      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:io   ar   sp   on   cti   bs   new   nbsp   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如何去掉多维数组的重复值

标签:io   ar   sp   on   cti   bs   new   nbsp   php   

原文地址:http://www.cnblogs.com/myphper/p/4113243.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!