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)
); 展开
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)
); 展开
2个回答
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);
?>
$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);
?>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
$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);
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);
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询