0

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.

Rob
  • 26,989
  • 16
  • 82
  • 98
AshBash
  • 27
  • 6
  • Can you add an example of your expected outcome? – mtr.web May 02 '18 at 13:09
  • @mtr.web Thx, have edited with an example & a bit more detail. – AshBash May 02 '18 at 14:11
  • Thanks for the edit. However, I meant an example of the resulting array. I think that you are looking for a final array that starts with the parent array, and adds a new field, such as `items` that would be an array of all the child items, **based on matching Trans_ID/tran_id** Can you verify? – mtr.web May 02 '18 at 14:19
  • Would you add to this question the code you are presently trying? I'm not quite sure if you are stuck on the code or just the file format. – halfer May 02 '18 at 18:31

1 Answers1

1

If your desired result is to have each child assigned to the existing parent-array, you can loop through each of them, add an items array to the parent, and check for the matching ids that will be added to the items array. This way there will always be an items, but if there are no matches then it will be empty, allowing you to run a similar loop for your output as well.

// load the child-items into your existing parent array
foreach ($parentArray as $key => $parent) {
    $parentArray[$key]['items'] = array();
    foreach($childArray as $child) {
        if ($parent['Trans_ID'] == $child['tran_id']) {
            $parentArray[$key]['items'][] = $child;
        }
    }
}

echo '<table>';
// loop through again for output 
foreach ($parentArray as $parent) {
    echo '<tr>';
    echo '<th>'.$parentArray['identifier'].'</th>';
    echo '<th>'.$parentArray['another-field'].'</th>';
    echo '<th>'.$parentArray['another-field'].'</th>';
    echo '</tr>';
    foreach ($parent['items'] as $item) {
        echo '<tr>';
        echo '<td>'.$item['sku'].'</td>';
        echo '<td>'.$item['another-field'].'</td>';
        echo '<td>'.$item['another-field'].'</td>';
        echo '</tr>';
    }
}
echo '</table>';

This may not be an exact fit, and you could alternatively use one nested loop for the output and skip the merging part, but this would enable you to use the array later, if desired

mtr.web
  • 1,505
  • 1
  • 13
  • 19
  • Interesting thank you. I'll have a look at working that in. I might be able to miss the output piece as once the array's are populated & merged, I can then pass them as variables to the fputcsv process to generate the output file for the ERP system to import. – AshBash May 02 '18 at 14:49