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中使用array_merge()合并数组时,如果包含空字符串或空数组,会产生令人困惑的结果。解决方案:1.使用array_filter()过滤空值。2.对于包含空数组的情况,使用递归合并函数array_merge_recursive_distinct()来保持一致的数组结构。

对于PHP中的数组合并,时间复杂度取决于算法:array_merge()和+运算符为O(m+n),其中m和n是数组大小。循环合并也是O(m+n)。根据数组大小和可用性等因素选择适当的方法,并考虑性能需求以优化应用程序。

PHP数组合并效率对比:Array_merge()、+运算符和Array_replace()这三个方法的时间复杂度均为O(n),表示合并时间与数组元素数量成正比。这三个方法的空间复杂度也是O(n),表示内存占用与数组元素数量成正比。实测结果表明,大数组合并时,Array_merge()和+运算符比Array_replace()更快。

在PHP中合并数组时,可以选择以下方法处理重复元素:使用array_merge()结合array_unique()去除重复元素。使用array_replace()覆盖重复元素而不改变原始数组。使用array_diff()剔除一个数组中不在另一个数组中的元素。

PHP5.5函数解析:如何使用array_reduce函数将数组元素合并为一个值在PHP编程中,我们经常需要对数组进行处理,有时候需要将数组的元素合并为一个值。这个时候,我们可以使用PHP5.5版本引入的array_reduce函数来实现这个功能。本文将详细介绍array_reduce函数的使用方法,并提供相应的代码示例。array_reduce函数是一

PHP数组是一种非常常用的数据结构,在开发中经常涉及到对数组的合并和分割操作。本文将介绍如何使用PHP语言实现这两种操作,并附上相应的代码示例。一、合并数组合并数组的操作可以使用array_merge()函数来实现。该函数接受多个数组作为参数,并将它们合并成一个新的数组。代码示例:$array1=["apple","ba

PHP中如何合并两个数组在PHP编程中,经常会遇到需要合并两个数组的情况。PHP提供了多种方法来实现数组的合并操作,本文将介绍其中的几种常用方法,并附带代码示例。方法一:使用array_merge函数array_merge函数是PHP提供的一个用于合并数组的内置函数。它接受多个数组作为参数,并返回一个合并后的新数组。以下是使用array_merge函数合并两

PHParray_merge()函数是一个用于合并多个数组的内置函数。该函数可以将多个数组合并成一个新的数组。在本文中,我们将讨论如何使用PHParray_merge()函数来合并多个数组。PHParray_merge()函数使用方法PHParray_merge()函数有多种用法,但是最常用的用法是将两个或更多数组合并为一个。下面是一个简单的示例:$


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SublimeText3 Mac version
God-level code editing software (SublimeText3)

SublimeText3 English version
Recommended: Win version, supports code prompts!

Zend Studio 13.0.1
Powerful PHP integrated development environment