PHP 二维数组根据其中某个键值判断,将有此值的项只保留一个
array([0]=>Array([user_id]=>71[brand_id]=>2[brand_name]=>(A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,...
array( [0] => Array (
[user_id] => 71
[brand_id] => 2
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[1] => Array
(
[user_id] => 71
[brand_id] => 789
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[2] => Array
(
[user_id] => 36
[brand_id] => 23
[brand_name] => (D)大众
)
[3] => Array
(
[user_id] => 35
[brand_id] => 2
[brand_name] => (A)奥迪
)
[4] => Array
(
[user_id] => 34
[brand_id] => 2
[brand_name] => (A)奥迪
)
)
例如上面的数组,将user_id 相等的项删掉,结果如下:
array( [0] => Array (
[user_id] => 71
[brand_id] => 2
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[1] => Array
(
[user_id] => 36
[brand_id] => 23
[brand_name] => (D)大众
)
[2] => Array
(
[user_id] => 35
[brand_id] => 2
[brand_name] => (A)奥迪
)
[3] => Array
(
[user_id] => 34
[brand_id] => 2
[brand_name] => (A)奥迪
)
) 展开
[user_id] => 71
[brand_id] => 2
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[1] => Array
(
[user_id] => 71
[brand_id] => 789
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[2] => Array
(
[user_id] => 36
[brand_id] => 23
[brand_name] => (D)大众
)
[3] => Array
(
[user_id] => 35
[brand_id] => 2
[brand_name] => (A)奥迪
)
[4] => Array
(
[user_id] => 34
[brand_id] => 2
[brand_name] => (A)奥迪
)
)
例如上面的数组,将user_id 相等的项删掉,结果如下:
array( [0] => Array (
[user_id] => 71
[brand_id] => 2
[brand_name] => (A)奥迪,(A)阿斯顿·马丁,(B)别克,(B)宾利,(B)标致,(B)本田,(B)奔腾,(B)北汽幻速,(B)宝骏,(B)布加迪
)
[1] => Array
(
[user_id] => 36
[brand_id] => 23
[brand_name] => (D)大众
)
[2] => Array
(
[user_id] => 35
[brand_id] => 2
[brand_name] => (A)奥迪
)
[3] => Array
(
[user_id] => 34
[brand_id] => 2
[brand_name] => (A)奥迪
)
) 展开
3个回答
展开全部
<?php
$arr = array(
array('user_id'=>1,'brand_id'=>'aaa','brand_name'=>'1'),
array('user_id'=>1,'brand_id'=>'aaa','brand_name'=>'2'),
array('user_id'=>1,'brand_id'=>'aaa','brand_name'=>'3'),
array('user_id'=>2,'brand_id'=>'ddd','brand_name'=>'4'),
array('user_id'=>3,'brand_id'=>'ddd','brand_name'=>'5')
);
//print_r($arr);
$newarr = array();
foreach($arr as $_arr){
if(!isset($newarr[$_arr['user_id']])){
$newarr[$_arr['user_id']] = $_arr;
}
}
print_r($newarr);
exit;
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询