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),
);
/*
要求把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)
);

第1个回答  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);
?>
第2个回答  2013-04-27
$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);本回答被提问者采纳

php 合并多维数组中相同的值
arr = array( array( array("stocknotifyId" => 1,"sku" => '13809_BL_37',"email" => 'huang@aaa.com'), array("stocknotifyId" => 3,"sku" => '13809_BL_37',"email" => 'hrheve@dfwef.com'), array("stocknotifyId" => 5,"sku" => '13809_BL_37',"e...

php取出多维数组重复的值
arr = array(array(array("stocknotifyId" => 1,"sku" => '13809_BL_37',"email" => 'huang@aaa.com'),array("stocknotifyId" => 3,"sku" => '13809_BL_37',"email" => 'hrheve@dfwef.com'),array("stocknotifyId" => 5,"sku" => '13809_BL_37',"email" => 'dfsfs...

PHP删除数组中的重复数据
这样的话,是有些麻烦啦,你得一项一项去比较才行,你先比较从2到9这几项的类型,然后再去比较每一项当中的数目,然后再比较每一项当中每一项的值是不是相同才行,比较相同要用“===”,而不是“==”,这一点得注意下;

懂php的人来帮我下,关于数组合并
'num'=>0),array('name'=>'cccccccccc','num'=>41)); $arr2=array(array('age'=>22),array('age'=>24),array('age'=>26)); foreach ($arr2 as $a) { foreach ($a as $aa) { $a1[]=$aa;

PHP多维数组排序,用array_multisort函数对数组按指定字段排序
在PHP中,有多种函数可用于对数组进行排序。例如,sort()函数用于数组的升序排列,而rsort()则用于降序排列。asort()和arsort()分别用于根据关联数组的值进行升序和降序排列,而ksort()和krsort()则是根据键进行排序。例如:可以得到以下排序结果:但是,当需要对具有多个字段的数组进行排序时,如何按照...

php两个相关的多维数组怎么合并成一个数组
php\/\/数组1(gc_class):$gc_class = array( 0 =>array('gc_id'=>1,'gc_name'=>'分类1', 'gc_title' => '111111'), 1 =>array('gc_id'=>2,'gc_name'=>'分类2', 'gc_title' => '2222'));\/\/数组2(acticity):$acticity = array( '0'=>array('activity_id...

php多维数组问题,,高分求高手帮帮,,
} arr =array('2'=>array('5'=>'ok','6'=>'good'),'3'=>array('7'=>'yes','8'=>'cool'),'4'=>array('9'=>'look'));output = fixArray($arr,"");print_r($output);2、你说的带主键名3,基本上不可能实现,因为如果键名相同,则值会互相覆盖,或者还是需要用2维数组。

php 多维数组键值重新排序
a = array(2=>array(1,2,3,4),6=>array(6,2,3,5),1=>array(1,4,53));b = array_values($a);\/\/返回数组中的所有值,形成新的数组,建立数字索引

php中 有一个数组array("0a","1b","2c","3d","4e","5f","6g","0a...
arr = array("0a","1b","2c","3d","4e","5f","6g","0a","2c","4e","6g");foreach($arr $k=>$v){ if(in_array($v,$arr)){ unset($arr[$k]);} } print_r($arr);貌似只适合一维 你可以用递归遍历数组向我这样处理应该可以适合多维数组 本人递归不精 ...

PHP如何对二维数组(多维数组)进排序
在PHP中,处理二维数组排序主要依赖于array_multisort()函数,该函数能根据指定键或值对数组进行排序。例如,要按照特定键“age”对二维数组进行升序排序,可以参考以下示例:示例代码会按照"age"键的升序对二维数组进行排序,并展示排序后的结果。对于其他键的排序,只需调整array_column()和array_multi...

相似回答