【发布时间】:2018-05-02 12:47:25
【问题描述】:
我一直在这里阅读这个问题,因为我遇到了类似的挑战:
Merging two multidimensional associative arrays
我有一个非常相似的问题,我试图将 2 个关联数组合并在一起,但它们在“父/子”模型中起作用,其中每个父数组可以有多个子数组。
我正在进行的工作是将基于云的新 EPOS 系统与需要以特定文件格式输出数据的旧 ERP 系统集成。每个事务都需要一个事务标题行 (TH),然后为 EPOS 事务 (TO) 中的每个项目嵌套行。
每个数组都是 Mysql 对视图的查询的结果,我编写该视图以允许在运行时进行更短的“选择”查询。我正在迭代多个循环以收集事务数据并将其作为一个单独的进程写入我的数据库,该进程将按计划运行。
作为源数据数组的例子如下(var_dump of each):
父记录示例:
array(1)
{ [0]=> array(53)
{ ["Identifier"]=> string(2) "TH"
["Trans_ID"]=> string(9) "157976911"
["Trans_Date_Time"]=> string(19) "2017-10-19 11:38:13"
["Till_ID"]=> string(5) "30481"
["TILL_NAME"]=> string(5) "Till1"
["Cashier_Name"]=> string(12) "Cashier_Name"
["Branch_Ref"]=> string(5) "16783"
["Order_Number"]=> string(0) ""
["Original_Invoice"]=> string(0) ""
["Returns_Number"]=> string(0) ""
["Obselete1"]=> string(0) ""
["Obselete2"]=> string(0) ""
["Obselete3"]=> string(0) ""
["Obselete4"]=> string(0) ""
["Obselete5"]=> string(0) ""
["Obselete6"]=> string(0) ""
["Obselete7"]=> string(0) ""
["Obselete8"]=> string(0) ""
["Trans_Type"]=> string(4) "till"
["Trans_Status"]=> string(4) "sold"
["Customer_ID"]=> NULL
["Obselete9"]=> string(0) ""
["Customer_Ref"]=> string(0) ""
["Cust_Surname"]=> NULL
["Cust_Forename"]=> NULL ["Title"]=> NULL
["Cust_add1"]=> string(0) ""
["Cust_add2"]=> string(0) ""
["Cust_City"]=> string(0) ""
["Cust_State"]=> string(0) ""
}
}
示例初始子记录 [0]:
array(2)
{ [0]=> array(30)
{ ["tran_id"]=> string(9) "157976911"
["row_head"]=> string(2) "TO"
["sku"]=> string(0) ""
["barcode"]=> string(0) ""
["item_name"]=> string(12) "Merlot Large"
["item_style"]=> string(21) "250ml glass of Merlot"
["qty"]=> string(1) "1"
}
}
如上所述,子数组适用于 EPOS 交易中的每个产品,因此一个父项可以有多个子数组。我现在已经在子 (TO) 数组中包含了事务 ID,但这必须从最终输出中省略。
因此,我在思考如何将多个 TO 行嵌套在每个相关的 TH 中时有些困难。
因此,管道分隔文件格式的示例如下:
TH|1|x|xx|x|xx|x|x
TO|1|x|xx|x|xx|x|x
TO|1|x|xx|x|xx|x|x
TH|2|x|xx|x|xx|x|x
TO|2|x|xx|x|xx|x|x
一切都在我的本地机器上写一个文件,但卡在这些数组上。
【问题讨论】:
-
你能添加一个你预期结果的例子吗?
-
@mtr.web 谢谢,已经编辑了一个例子和更多细节。
-
感谢您的编辑。但是,我的意思是结果数组的一个例子。我认为您正在寻找一个以父数组开头的最终数组,并添加一个新字段,例如
items这将是所有子项的数组,基于匹配的 Trans_ID/tran_id 你能验证吗? -
您会在这个问题中添加您目前正在尝试的代码吗?我不太确定你是卡在代码上还是只是文件格式。
标签: php arrays multidimensional-array merging-data