当前位置: 代码迷 >> PHP >> 数组操作解决思路
  详细解决方案

数组操作解决思路

热度:192   发布时间:2012-04-11 17:42:33.0
数组操作
PHP code
array (
  'time' => '2012-03-05 13',
  'type' => 'ETC56-605N',
  'count' => '52',
)
array (
  'time' => '2012-03-05 14',
  'type' => 'ETC56-605N',
  'count' => '8',
)
array (
  'time' => '2012-03-05 14',
  'type' => 'ETC56-542N',
  'count' => '39',
)
array (
  'time' => '2012-03-05 15',
  'type' => 'ETC56-542N',
  'count' => '41',
)
array (
  'time' => '2012-03-05 15',
  'type' => 'ETC61-560N',
  'count' => '10',
)

如何变成array(time=>array(type=>count))这种形式?

------解决方案--------------------
这样?
PHP code
$arr = array(
        array (
          'time' => '2012-03-05 13',
          'type' => 'ETC56-605N',
          'count' => '52',
        ),  
        array (
          'time' => '2012-03-05 14',
          'type' => 'ETC56-605N',
          'count' => '8',
        ),  
        array (
          'time' => '2012-03-05 14',
          'type' => 'ETC56-542N',
          'count' => '39',
        ),  
        array (
          'time' => '2012-03-05 15',
          'type' => 'ETC56-542N',
          'count' => '41',
        ),  
        array (
          'time' => '2012-03-05 15',
          'type' => 'ETC61-560N',
          'count' => '10',
        )   
);
foreach($arr as $array)
{
        $newarr[$array['time']][$array['type']] += $array['count'];
}
print_r($newarr); 
  相关解决方案