I have been reading this question here as I have a similar challenge:
Merging two multidimensional associative arrays
I have a very similar problem where I am trying to merge 2 associative arrays together but they act in a 'parent/child' model where there can be multiple child arrays for each parent array.
The piece I am working on is to integrate a new cloud based EPOS system with a legacy ERP that requires the data to be output in a specific file format. Each transaction requires a Transaction Header row (TH) and then nested rows for each item within the EPOS transaction (TO).
Each array is the result of a Mysql query on views which I wrote to allow for shorter 'select' queries at run time. I am iterating through multiple loops to gather the transactional data and write it to my database as a seperate process that will run on a schedule.
As example of the source data arrays are as follows (var_dump of each):
Example parent record:
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) ""
}
}
And Example Initial Child Records [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"
}
}
As per above the child arrays are for each product on an EPOS transactions so there can be multiple child arrays to one parent. I have included the transaction ID within the child (TO), arrays for now but this has to be ommitted from the final output.
As a result, I'm struggling slightly to think how I can nest the multiple TO rows within each relevant TH.
So an example of the pipe delimited file format would be:
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
Everything is working to write a file on my local machine but stuck on these arrays.