$list=array(
0=>array('trace_id'=>1,'trans_card_num'=>'001','total_fee'=>1),
1=>array('trace_id'=>2,'trans_card_num'=>'002','total_fee'=>2),
2=>array('trace_id'=>3,'trans_card_num'=>'003','total_fee'=>3),
3=>array('trace_id'=>4,'trans_card_num'=>'004','total_fee'=>4),
4=>array('trace_id'=>5,'trans_card_num'=>'005','total_fee'=>5),
5=>array('trace_id'=>6,'trans_card_num'=>'005','total_fee'=>6),
6=>array('trace_id'=>7,'trans_card_num'=>'005','total_fee'=>7),
7=>array('trace_id'=>8,'trans_card_num'=>'005','total_fee'=>8),
);
$ar=array();
foreach($list as $v){
if(empty($ar) || !in_array($v,$ar)){
$ar[$v['trans_card_num']][$v['trace_id']]=$v;
}else{
foreach($ar as $ka=>$va){
if($ka==$v['trans_card_num']){
$ar[$ka][$v['trace_id']]=$v;
}
}
}
}
foreach($ar as $k=>$v){
/*
if(is_array($v)){
foreach($v as $key=>$val){
if(is_array($val)){
$val['num'] = count($v);
}
$v[$key] = $val;
}
$ar[$k] = $v;
}*/
$sum = 0 ;
foreach($v as $key=>$val){
if(is_array($val)){
$val['num'] = count($v);
if(isset($val['total_fee'])){
$sum += $val['total_fee'];
}
}
$v[$key] = $val;
}
foreach($v as $key=>$val){
if(is_array($val)){
$val['total_price'] = $sum;
}
$v[$key] = $val;
}
$ar[$k] = $v;
}
$item = array();
foreach($ar as $k=>$v){
if(is_array($v)){
$i=0;
$item_arr = array();
foreach($v as $key=>$val){
$item_arr[$i] = $val;
$i++;
}
$item[] = $item_arr;
}
}
$foo = array();
foreach($item as $k=>$v){
if(is_array($v)){
$foo[] = $v[0];
}
}
echo '<pre>';
print_r($foo);
0=>array('trace_id'=>1,'trans_card_num'=>'001','total_fee'=>1),
1=>array('trace_id'=>2,'trans_card_num'=>'002','total_fee'=>2),
2=>array('trace_id'=>3,'trans_card_num'=>'003','total_fee'=>3),
3=>array('trace_id'=>4,'trans_card_num'=>'004','total_fee'=>4),
4=>array('trace_id'=>5,'trans_card_num'=>'005','total_fee'=>5),
5=>array('trace_id'=>6,'trans_card_num'=>'005','total_fee'=>6),
6=>array('trace_id'=>7,'trans_card_num'=>'005','total_fee'=>7),
7=>array('trace_id'=>8,'trans_card_num'=>'005','total_fee'=>8),
);
$ar=array();
foreach($list as $v){
if(empty($ar) || !in_array($v,$ar)){
$ar[$v['trans_card_num']][$v['trace_id']]=$v;
}else{
foreach($ar as $ka=>$va){
if($ka==$v['trans_card_num']){
$ar[$ka][$v['trace_id']]=$v;
}
}
}
}
foreach($ar as $k=>$v){
/*
if(is_array($v)){
foreach($v as $key=>$val){
if(is_array($val)){
$val['num'] = count($v);
}
$v[$key] = $val;
}
$ar[$k] = $v;
}*/
$sum = 0 ;
foreach($v as $key=>$val){
if(is_array($val)){
$val['num'] = count($v);
if(isset($val['total_fee'])){
$sum += $val['total_fee'];
}
}
$v[$key] = $val;
}
foreach($v as $key=>$val){
if(is_array($val)){
$val['total_price'] = $sum;
}
$v[$key] = $val;
}
$ar[$k] = $v;
}
$item = array();
foreach($ar as $k=>$v){
if(is_array($v)){
$i=0;
$item_arr = array();
foreach($v as $key=>$val){
$item_arr[$i] = $val;
$i++;
}
$item[] = $item_arr;
}
}
$foo = array();
foreach($item as $k=>$v){
if(is_array($v)){
$foo[] = $v[0];
}
}
echo '<pre>';
print_r($foo);
echo '</pre>';
输出的结果是
Array ( [0] => Array ( [trace_id] => 1 [trans_card_num] => 001 [total_fee] => 1 [num] => 1 [total_price] => 1 ) [1] => Array ( [trace_id] => 2 [trans_card_num] => 002 [total_fee] => 2 [num] => 1 [total_price] => 2 ) [2] => Array ( [trace_id] => 3 [trans_card_num] => 003 [total_fee] => 3 [num] => 1 [total_price] => 3 ) [3] => Array ( [trace_id] => 4 [trans_card_num] => 004 [total_fee] => 4 [num] => 1 [total_price] => 4 ) [4] => Array ( [trace_id] => 5 [trans_card_num] => 005 [total_fee] => 5 [num] => 4 [total_price] => 26 ) )