有这样一个数组:
$arr1 = array(
array("username"=>"piaoyudesi","email"=>"piaoyudesi@163.com","address"=>"shenzhen","item_id"=>"10058","item_title"=>"BPS8"),
array("username"=>"huaka","email"=>"huakai@163.com","address"=>"zhongshan","item_id"=>"10087","item_title"=>"BPS9"),
array("username"=>"huakai","email"=>"huakai@163.com","address"=>"zhongshan","item_id"=>"10047","item_title"=>"BPS10"),
array("username"=>"piaoyudesi","email"=>"piaoyudesi@163.com","address"=>"shenzhen","item_id"=>"10042","item_title"=>"BPS10"),
array("username"=>"jimowenxin","email"=>"jimowenxin@163.com","address"=>"zhuhai","item_id"=>"10031","item_title"=>"BPS18")
);
数组中有用户信息username,email,address,产品信息item_id,item_title
也就是同一个用户购买了不同的产品,现在要把username相同的值合并,但是item_id,item_title不能合并
合并之后最好是这样的数组
array("username=>"piaoyudesi",
"email"=>"piaoyudesi@163.com",
"address"=>"shenzhen",
"goods"=>array(array("item_id"=>"10058","item_title"=>BPS8"),
array("item_id"=>"10047", "item_title"=>BPS10")
)
array("username=>"huakai",
"email"=>"huakai@163.com",
"address"=>"zhongshan",
"goods"=>array(array("item_id"=>"10087", "item_title"=>BPS9"),
array("item_id"=>"10047", "item_title"=>BPS10")
)
array("username=>"jimowenxin",
"email"=>"jimowenxin@163.com",
"address"=>"zhuhai",
"goods"=>array(array("item_id"=>"10031", "item_title"=>BPS18")
)
php将数组键值相同的数组合并数组为一个数组
答案:1 悬赏:30 手机版
解决时间 2021-04-04 15:41
- 提问者网友:浩歌待明月
- 2021-04-04 08:56
最佳答案
- 五星知识达人网友:煞尾
- 2021-04-04 10:32
把'huaka'改成了'huakai'似乎是一个人
array("username"=>"piaoyudesi","email"=>"piaoyudesi@163.com","address"=>"shenzhen","item_id"=>"10058","item_title"=>"BPS8"),
array("username"=>"huakai","email"=>"huakai@163.com","address"=>"zhongshan","item_id"=>"10087","item_title"=>"BPS9"),
array("username"=>"huakai","email"=>"huakai@163.com","address"=>"zhongshan","item_id"=>"10047","item_title"=>"BPS10"),
array("username"=>"piaoyudesi","email"=>"piaoyudesi@163.com","address"=>"shenzhen","item_id"=>"10042","item_title"=>"BPS10"),
array("username"=>"jimowenxin","email"=>"jimowenxin@163.com","address"=>"zhuhai","item_id"=>"10031","item_title"=>"BPS18")
);
$u=array();
foreach($a as $k=>&$e){
$name=&$e['username'];
if(!isset($u[$name])){
$u[$name]=$e;
unset($u[$name]['item_id'],$u[$name]['item_title']);
}
$u[$name]['goods'][]=array('item_id'=>$e['item_id'],'item_title'=>$e['item_title']);
}
$a=array_values($u); unset($u);
print_r($a);
=========
Array
(
[0] => Array
(
[username] => piaoyudesi
[email] => piaoyudesi@163.com
[address] => shenzhen
[goods] => Array
(
[0] => Array
(
[item_id] => 10058
[item_title] => BPS8
)
[1] => Array
(
[item_id] => 10042
[item_title] => BPS10
)
)
)
[1] => Array
(
[username] => huakai
[email] => huakai@163.com
[address] => zhongshan
[goods] => Array
(
[0] => Array
(
[item_id] => 10087
[item_title] => BPS9
)
[1] => Array
(
[item_id] => 10047
[item_title] => BPS10
)
)
)
[2] => Array
(
[username] => jimowenxin
[email] => jimowenxin@163.com
[address] => zhuhai
[goods] => Array
(
[0] => Array
(
[item_id] => 10031
[item_title] => BPS18
)
)
)
)
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯