php将数组键值相同的数组合并数组为一个数组
有这样一个数组:$arr1=array(array("username"=>"piaoyudesi","email"=>"piaoyudesi@163.com","add...
有这样一个数组:
$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")
) 展开
$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")
) 展开
1个回答
展开全部
把'huaka'改成了'huakai'似乎是一个人
<?php
$a=array(
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
)
)
)
)
<?php
$a=array(
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
)
)
)
)
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询