首頁 > 後端開發 > php教程 > 数组合并

数组合并

WBOY
發布: 2016-06-23 14:20:17
原創
910 人瀏覽過

2个数组:

//数组aarray (  'SU13080800340' =>   array (    0 => 'CVT121015001',    1 => 'CVT121015002',    2 => 'CVT121226001',  ),)//数组barray (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '3',  'total' => '48',  'c1_time' => '2013-08-10 15:00:00',)
登入後複製


求达到合并的效果:
array ('0' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '3',  'total' => '48',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121015001',),'1' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '3',  'total' => '48',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121015002',),'2' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '3',  'total' => '48',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121016001',),)
登入後複製


回复讨论(解决方案)

楼主你应该更明确的描述你的需求

如果仅仅是按照你的样例来写代码,扩展起来可能又会出现问题

按照你的样例,可以这么写:


//数组a$a = array (  'SU13080800340' =>   array (    0 => 'CVT121015001',    1 => 'CVT121015002',    2 => 'CVT121226001'  ));//数组b$b = array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '3',  'total' => '48',  'c1_time' => '2013-08-10 15:00:00');$res = array();foreach($a['SU13080800340'] as $key=>$each){	$res[] = $b;	$res[$key]['packageno'] = $each;}var_export($res);
登入後複製


结果:
array (  0 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5A',    'mount_total' => '3',    'total' => '48',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015001',  ),  1 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5A',    'mount_total' => '3',    'total' => '48',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5A',    'mount_total' => '3',    'total' => '48',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121226001',  ),)
登入後複製

楼主你应该更明确的描述你的需求
如果仅仅是按照你的样例来写代码,扩展起来可能又会出现问题
我看了下数据结构,stockno对应的packageno的结果如数组a所示,数组b是数据表按照cust_no进行group by,例子更新下:

//数组aarray (  'SU13080800340' =>   array (    0 => 'CVT121015001',    1 => 'CVT121015002',    2 => 'CVT121226001',  ),)//数组barray ('0' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '1',  'total' => '16',  'c1_time' => '2013-08-10 15:00:00',),'1' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5B',  'mount_total' => '2',  'total' => '32',  'c1_time' => '2013-08-10 15:00:00',),)
登入後複製


求达到合并的效果:
array ('0' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '1',  'total' => '16',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121015001',),'1' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '2',  'total' => '32',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121015002',),'2' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '2',  'total' => '32',  'c1_time' => '2013-08-10 15:00:00',  'packageno' => 'CVT121016001',),)
登入後複製

没看出来你这两个数组之间是如何判断联系的.

//数组a$a = array (  'SU13080800340' =>   array (    0 => 'CVT121015001',    1 => 'CVT121015002',    2 => 'CVT121226001',  ),);//数组b$b = array ('0' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '1',  'total' => '16',  'c1_time' => '2013-08-10 15:00:00',),'1' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5B',  'mount_total' => '2',  'total' => '32',  'c1_time' => '2013-08-10 15:00:00',),);foreach($b as $item) {  for($i=0; $i<$item['mount_total']; $i++) {    $item['packageno'] = current(array_splice($a[$item['stock_no']], 0, 1));    $res[] = $item;  }}var_export($res);
登入後複製
登入後複製
array (  0 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5A',    'mount_total' => '1',    'total' => '16',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015001',  ),  1 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121226001',  ),)
登入後複製
登入後複製

//数组a$a = array (  'SU13080800340' =>   array (    0 => 'CVT121015001',    1 => 'CVT121015002',    2 => 'CVT121226001',  ),);//数组b$b = array ('0' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5A',  'mount_total' => '1',  'total' => '16',  'c1_time' => '2013-08-10 15:00:00',),'1' =>array (  'stock_no' => 'SU13080800340',  'adress' => 'B',  'arr_time' => '2013-08-14 09:00:00',  'c_type' => 'P32E',  'cust_no' => '310F61VA5B',  'mount_total' => '2',  'total' => '32',  'c1_time' => '2013-08-10 15:00:00',),);foreach($b as $item) {  for($i=0; $i<$item['mount_total']; $i++) {    $item['packageno'] = current(array_splice($a[$item['stock_no']], 0, 1));    $res[] = $item;  }}var_export($res);
登入後複製
登入後複製
array (  0 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5A',    'mount_total' => '1',    'total' => '16',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015001',  ),  1 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121015002',  ),  2 =>   array (    'stock_no' => 'SU13080800340',    'adress' => 'B',    'arr_time' => '2013-08-14 09:00:00',    'c_type' => 'P32E',    'cust_no' => '310F61VA5B',    'mount_total' => '2',    'total' => '32',    'c1_time' => '2013-08-10 15:00:00',    'packageno' => 'CVT121226001',  ),)
登入後複製
登入後複製


版大这样也只是做出来他举的特定例子吧...  没法判断数组A和B就是这种恰好顺序对应的关系啊...

版大这样也只是做出来他举的特定例子吧...  没法判断数组A和B就是这种恰好顺序对应的关系啊...

关系是写死的。谢谢了!

相關標籤:
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板