合并多个数组php

时间:2011-01-24 00:39:57

标签: php arrays merge

我真的需要一些帮助来解决这个问题。

我有很多阵列,我想合并其中一些。看起来如下。

            while ($row = $db->fetch_array($result_set)) {
                $year           = $row['year(Tidspunkt)'];
                $month_num      = $row['month(Tidspunkt)'];
                $month      = $cal->name_of_month($row['month(Tidspunkt)']);
                $type           = $row['Klubtype'];
                $visits         = $row['count(Handling)'];
                $days_in_month = $cal->days_in_month($month_num,$year);
                $avg           = $visits / $days_in_month;
                $object_array[]= array('month' => $month , 'visits' => $visits, 'type' => $type, 'avg' => $avg);
            }
            return $month_array;

输出看起来像这样

Array ( 
     [0] => Array ( [month] => Januar [visits] => 891 [type] => FK [avg] => 28.7419354839 ) 
     [1] => Array ( [month] => Januar [visits] => 23 [type] => UK [avg] => 0.741935483871 ) 
)

现在我想根据month的值合并这两个数组。想象一下,当我有一整年的阵列。那么有12个数组而不是24个很好。

感谢您帮助我。

1 个答案:

答案 0 :(得分:1)

我认为这可能是你想要的......

$newArray = array();

foreach($array as $value) {

   $month = $value['month'];
   unset($value['month']);
   $newArray[$month][] = $value;
}

这会给你类似......

Array ( 
     ['Januar'] => Array (
        [0] => Array( [visits] => 891 [type] => FK [avg] => 28.7419354839 ) 
        [1] => Array ( [visits] => 23 [type] => UK [avg] => 0.741935483871 ) 
)