數組合并問題(已更新)

來源:互聯網
上載者:User
array (  0 =>   array (    'cust_no' => '237109S92B',    'hi_no' => 'MEC38-431',    'arr_time' => '30/03/2014  9:00',    'totals' => 15,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 1,    'c_type' => 'D22',  ),  1 =>   array (    'cust_no' => '237109S92B',    'hi_no' => 'MEC38-431',    'arr_time' => '30/03/2014  9:00',    'totals' => 15,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 1,    'c_type' => 'D22',  ),  2 =>   array (    'cust_no' => '237033AW0A',    'hi_no' => 'BEM330-500',    'arr_time' => '30/03/2014 19:00',    'totals' => 15,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 1,    'c_type' => 'X11M',  ),  3 =>   array (    'cust_no' => '237033AW0A',    'hi_no' => 'BEM330-500',    'arr_time' => '30/03/2014 19:00',    'totals' => 45,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 3,    'c_type' => 'X11M',  ),  4 =>   array (    'cust_no' => '237033AW0A',    'hi_no' => 'BEM330-500',    'arr_time' => '30/03/2014 19:00',    'totals' => 45,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 3,    'c_type' => 'X11M',  ), 5 =>   array (    'cust_no' => '237033AW0A',    'hi_no' => 'BEM330-500',    'arr_time' => '1/04/2014 19:00',    'totals' => 45,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 3,    'c_type' => 'X11M',  ),)



條件:當arr_time和c_type,snp都相同時,把條件相同的數組合并為新的二維數組,並設定日期形式yyyy-mm-dd_$i為其序號。變成例如
array (  0 =>   array (    'no'=> '2014-04-08_1',    'cust_no' => '237109S92B',    'hi_no' => 'MEC38-431',    'arr_time' => '30/03/2014  9:00',    'totals' => 15,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 1,    'c_type' => 'D22',  ),  1 =>   array (    'no'=> '2014-04-08_1',    'cust_no' => '237109S92B',    'hi_no' => 'MEC38-431',    'arr_time' => '30/03/2014  9:00',    'totals' => 15,    'ch_date' => '26/03/2014  0:00',    'snp' => 15,    'mount' => 1,    'c_type' => 'D22',  ),)

請問怎麼求解?


回複討論(解決方案)

$res = array();foreach($ar as $t) {  $k = join('_', array($t['arr_time'], $t['c_type'], $t['snp']));  $res[$k][] = $t;}print_r($res);
Array(    [30/03/2014  9:00_D22_15] => Array        (            [0] => Array                (                    [cust_no] => 237109S92B                    [hi_no] => MEC38-431                    [arr_time] => 30/03/2014  9:00                    [totals] => 15                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 1                    [c_type] => D22                )            [1] => Array                (                    [cust_no] => 237109S92B                    [hi_no] => MEC38-431                    [arr_time] => 30/03/2014  9:00                    [totals] => 15                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 1                    [c_type] => D22                )        )    [30/03/2014 19:00_X11M_15] => Array        (            [0] => Array                (                    [cust_no] => 237033AW0A                    [hi_no] => BEM330-500                    [arr_time] => 30/03/2014 19:00                    [totals] => 15                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 1                    [c_type] => X11M                )            [1] => Array                (                    [cust_no] => 237033AW0A                    [hi_no] => BEM330-500                    [arr_time] => 30/03/2014 19:00                    [totals] => 45                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 3                    [c_type] => X11M                )            [2] => Array                (                    [cust_no] => 237033AW0A                    [hi_no] => BEM330-500                    [arr_time] => 30/03/2014 19:00                    [totals] => 45                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 3                    [c_type] => X11M                )        )    [1/04/2014 19:00_X11M_15] => Array        (            [0] => Array                (                    [cust_no] => 237033AW0A                    [hi_no] => BEM330-500                    [arr_time] => 1/04/2014 19:00                    [totals] => 45                    [ch_date] => 26/03/2014  0:00                    [snp] => 15                    [mount] => 3                    [c_type] => X11M                )        ))
接下來該怎麼做,你應該會的
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.