项目描述:
我需要从A同事那里拿到一部分数据a,从B同事那里拿到另一部分数据b
他们之前关联字段是一个动态的md5字符串(唯一标识)
通过从B同事的表,查一条数据的md5,根据这条md5去A同事那拿a数据
如果channel一致,要将下面的字段进行累加
//假设我已经将数据合并
$result = array(
array(
'channel'=>'zhang',
'sex' => '1',
'age' => '2',
'my_age' =>-1,
'm'=>1,
'num'=>1
),
array(
'channel'=>'zhang',
'sex'=>'1',
'age'=>'3',
'my_age'=>2,
'm'=>'2',
'num'=>5
),
array(
'channel'=>'wang',
'sex'=>'1',
'age'=>'3',
'my_age'=>2,
'm'=>'10',
'num'=>15
),
);
//根据channel将数组进行划分
$new = array();
foreach ($result as $k=>$v)
{
if(isset($new[$v['channel']])){
$new[$v['channel']]['num'] += $v['num'];
$new[$v['channel']]['m'] += $v['m'];
$new[$v['channel']]['channel'] = $v['channel'];
}else{
$new[$v['channel']]['num'] = $v['num'];
$new[$v['channel']]['m'] += $v['m'];
$new[$v['channel']]['channel'] = $v['channel'];
}
}
//如果数组不为空
function transArr($arr=array())
{
$i=0;
if(empty($arr))
{
return false;
}
foreach($arr as $key => $value)
{
$newArr[$i] = $value;
$i++;
}
return $newArr;
}
//转为索引数组
$data = transArr($new);
var_dump($data);