21,886
社区成员
发帖
与我相关
我的任务
分享
//原始的数组如下:
$test=array(
'titan'=>array(0=>array('date'=>'2011-08-11','DAU'=>'222','DNU'=>'111','DNUF'=>'444'),
1=>array('date'=>'2011-08-12','DAU'=>'333','DNU'=>'222','DNUF'=>'333'),
2=>array('date'=>'2011-08-13','DAU'=>'444','DNU'=>'333','DNUF'=>'555')),
'apollo'=>array(0=>array('date'=>'2011-08-11','DAU'=>'298','DNU'=>'290','DNUF'=>'900'),
1=>array('date'=>'2011-08-12','DAU'=>'340','DNU'=>'490','DNUF'=>'599'),
2=>array('date'=>'2011-08-13','DAU'=>'234','DNU'=>'345','DNUF'=>'532')),
'bossvegas'=>array(0=>array('date'=>'2011-08-11','DAU'=>'343','DNU'=>'532','DNUF'=>'342'),
1=>array('date'=>'2011-08-12','DAU'=>'323','DNU'=>'234','DNUF'=>'521'),
2=>array('date'=>'2011-08-13','DAU'=>'342','DNU'=>'342','DNUF'=>'523')),
'fishbowl'=>array(0=>array('date'=>'2011-08-11','DAU'=>'342','DNU'=>'523','DNUF'=>'522'),
1=>array('date'=>'2011-08-12','DAU'=>'333','DNU'=>'324','DNUF'=>'555'),
2=>array('date'=>'2011-08-13','DAU'=>'943','DNU'=>'533','DNUF'=>'666')),
);
//需要合并成如下的形式:
$resultArray=array(
0=>array(0=>array('date'=>'2011-08-11','DAU-titan'=>'222','DAU-apollo'=>'298','DAU-bossvegas'=>'','DAU-fishbowl'=>''),
1=>array('date'=>'2011-08-12','DAU-titan'=>'','DAU-apollo'=>'','DAU-bossvegas'=>'','DNU-fishbowl'=>''),
2=>array('date'=>'2011-08-13','DAU-titan'=>'','DAU-apollo'=>'','DAU-bossvegas'=>'','DAU-fishbowl'=>'')),
1=>array(0=>array('date'=>'2011-08-11','DNU-titan'=>'111','DNU-apollo'=>'290','DNU-bossvegas'=>'','DNU-fishbowl'=>''),
1=>array('date'=>'2011-08-12','DNU-titan'=>'','DNU-apollo'=>'','DNU-bossvegas'=>'','DNU-fishbowl'=>''),
2=>array('date'=>'2011-08-13','DNU-titan'=>'','DNU-apollo'=>'','DNU-bossvegas'=>'','DNU-fishbowl'=>'')),
2=>array(0=>array('date'=>'2011-08-11','DNUF-titan'=>'444','DNUF-apollo'=>'900','DAUF-bossvegas'=>'','DNUF-fishbowl'=>''),
1=>array('date'=>'2011-08-12','DNUF-titan'=>'','DNUF-apollo'=>'','DAUF-bossvegas'=>'','DNUF-fishbowl'=>''),
2=>array('date'=>'2011-08-13','DNUF-titan'=>'','DNUF-apollo'=>'','DAUF-bossvegas'=>'','DAUF-fishbowl'=>''))
);
$gamelist=array(0=>array('date','DAU-apollo','DAU-bossvegas','DAU-fishbowl','DAU-titan'),
1=>array('date','DNU-apollo','DNU-bossvegas','DNU-fishbowl','DNU-titan'),
2=>array('date','DNUF-apollo','DNUF-bossvegas','DNUF-fishbowl','DNUF-titan'));
for($i=0;$i<=2;$i++)
for($j=0;$j<=2;$j++)
foreach($gamelist[$i] as $k)
{
if($k=='date')
$result[$i][$j]['date'] = $test['titan'][$j]['date'];
else
{
$str = substr($k,4);
$result[$i][$j][$k] = $test[$str][$j][$k];
}
}
echo '<pre>';
var_export($result);
echo '</pre>';
$result = array();
$i = 0;
while($ar = current($test)) {
$key = key($test);
$result[$i] = array();
foreach($ar as $v) {
//print_r($v);
$ar_keys = array_keys($v);
$ar_values = array_values($v);
for($j = 1; $j < count($ar_keys); $j ++) $ar_keys[$j] = $key.$ar_keys[$j];
array_push($result[$i], array_combine($ar_keys, $ar_values));
}
$i ++;
next($test);
}
print_r($result);