php 把多维数组中,某值相同的项合并,重组数据

$laArray=array(0=>array("id"=>11,"name"=>"happy","age"=>"20","moeny"=>100),1=>array("... $laArray = array(
0 => array("id"=>11,"name"=>"happy","age"=>"20","moeny"=>100),
1 => array("id"=>29,"name"=>"lucy","age"=>"20","moeny"=>100),
2 => array("id"=>34,"name"=>"happy","age"=>"20","moeny"=>235),
3 => array("id"=>42,"name"=>"happy","age"=>"15","moeny"=>100),
4 => array("id"=>51,"name"=>"happy","age"=>"20","moeny"=>600),
5 => array("id"=>61,"name"=>"lucy","age"=>"20","moeny"=>350),
6 => array("id"=>61,"name"=>"lucy","age"=>"23","moeny"=>150),
);
/*
要求把name和age都一致的项合并,重组数组,以键值较先的数组为主,合并的数组间id用+号相连,name和age当然一致,moeny则变成合并间的项的新数组:
如上数组合并后为:
*/
$laArray = array(
0 => array("id"=>"11 + 34 + 51","name"=>"happy","age"=>"20","moeny"=>array(0=>100,1=>235,2=>600)),
1 => array("id"=>"29 + 61","name"=>"lucy","age"=>"20","moeny"=>array(0=>100,1=>350)),
//2 => array("id"=>34,"name"=>"happy","age"=>"20","moeny"=>235),
2 => array("id"=>42,"name"=>"happy","age"=>"15","moeny"=>100),
//4 => array("id"=>51,"name"=>"happy","age"=>"20","moeny"=>600),
//5 => array("id"=>61,"name"=>"lucy","age"=>"20","moeny"=>350),
3 => array("id"=>61,"name"=>"lucy","age"=>"23","moeny"=>150)
);
展开
 我来答
匿名用户
2013-04-27
展开全部
<?php
$laArray = array(
0 => array("id"=>11,"name"=>"happy","age"=>"20","moeny"=>100),
1 => array("id"=>29,"name"=>"lucy","age"=>"20","moeny"=>100),
2 => array("id"=>34,"name"=>"happy","age"=>"20","moeny"=>235),
3 => array("id"=>42,"name"=>"happy","age"=>"15","moeny"=>100),
4 => array("id"=>51,"name"=>"happy","age"=>"20","moeny"=>600),
5 => array("id"=>61,"name"=>"lucy","age"=>"20","moeny"=>350),
6 => array("id"=>61,"name"=>"lucy","age"=>"23","moeny"=>150),
);
$temArr = $laArray ;
$newArr = array();
foreach($laArray as $num => $arr ){
unset($temArr[$num]);
$id = $arr['id'];
foreach($temArr as $tNum =>$tArr ){
$n_arr = array();
$tem_moeny = array();
if($arr['name']== $tArr['name'] && $arr['age']== $tArr['age']){
$id .= '+'.$tArr['id'];
$n_arr['id'] = $id;
$n_arr['name'] = $arr['name'];
$n_arr['age'] = $arr['age'];
if(is_array($arr['moeny'])){
array_push($n_arr['moeny'], $tArr['moeny']);
}else{
$tem_moeny [] = $arr['moeny'];
$tem_moeny [] = $tArr['moeny'];
}
$tem_moeny[] = $tArr['moeny'];
$n_arr['moeny'] = $tem_moeny;
$newArr[] = $n_arr ;
}
}
if($id == $arr['id']){
$newArr[] = $arr ;
}
}

print_r($newArr);
?>
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
谏豆f2
2013-04-27 · TA获得超过201个赞
知道小有建树答主
回答量:413
采纳率:0%
帮助的人:324万
展开全部
$tmpArray = array();
foreach ($laArray as $row) {
$key = $row['name'] . $row['age'];
if (array_key_exists($key, $tmpArray)) {
$tmpArray[$key]['id'] = $tmpArray[$key]['id'] . '+' . $row['id'];
if (is_array($tmpArray[$key]['moeny'])) {
$tmpArray[$key]['moeny'][] = $row['moeny'];
} else {
$tmpArray[$key]['moeny'] = array($tmpArray[$key]['moeny'], $row['moeny']);
}
} else {
$tmpArray[$key] = $row;
}
}

echo "<pre>";
var_dump($tmpArray);
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式