0

I'm exporting from MySQL into an Excel spreadsheet, the export works fine however I'm trying to change how the output works from the array.

Example of the array

Array
(
[0] => Array
    (
        [Product] => ABYT8
        [Invoice Date] => 24/04/2018
        [Carriage] => 31.00
        [Carriage Tax Code] => T0
        [Order Number] => 223
    )

[1] => Array
    (
        [Product] => CASTSRF
        [Invoice Date] => 24/04/2018
        [Carriage] => 0.00
        [Carriage Tax Code] => T1
        [Order Number] => 224
    )

[2] => Array
    (
        [Product] => 12K816
        [Invoice Date] => 24/04/2018
        [Carriage] => 0.00
        [Carriage Tax Code] => T1
        [Order Number] => 224
    )
)

Array 0 is for Sale 223 and Array 1 & 2 are for Sale 224.

The foreach that loops through this array and outputs to Excel is as follows:

foreach ($export_array as $row) {
    if (!$flag) {
      // display field/column names as first row
        echo implode("\t", array_keys($row)) . "\n";
        $flag = true;
    }       
    array_walk($row, __NAMESPACE__ . '\cleanData');
    echo implode("\t", array_values($row)) . "\n";
}

I only want to output the Carriage & Carriage Tax Code values on the first iteration for each Sales Order Number but I don't know how to do this. I tried re writing the entire thing which grouped the data into a multidimensional array that was grouped by Order Number, which worked but then I failed in getting the export to work.

2
  • Then you are going to have to add a test so you only print what you want Commented Apr 24, 2018 at 15:34
  • @RiggsFolly Yes you are correct, I need to test/check whether the array key "Order Number" has changed and if a change is detected then either print or don't print the array keys "Carriage" & "Carriage Tax Code". Or would it be better to modify the array prior to the Output? By this I mean leave the Carriage & Carriage Tax keys as they are for the first iteration and then empty the keys for subsequent keys that have the same Order Number? Commented Apr 24, 2018 at 15:40

1 Answer 1

5

General pattern for keeping track of changing groups as you iterate an array:

// keep track of the previous value of the group column as you iterate
$previousOrderNumber = null;

foreach ($export_array as $index => $row) {
    if ($row['Order Number'] != $previousOrderNumber) {
        // it's a new order number, so print the Carriage stuff
    } else {
        // it's the same order number, so don't
    }

    // current becomes previous
    $previousOrderNumber = $row['Order Number'];
}

This does depend on the array being sorted by the group column (Order Number in this case), so be sure that's done in your query, or usort the array if it doesn't come from a database.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this, definitely a step forwards. The problem I'm having now is how I selectively output the array values. At present I use echo implode("\t", array_values($row)) . "\n"; I need to implode the array so the Excel spreadsheet is formatted/created correctly. As a test I tried using array_column but this didn't work, there wasn't an error, just no output. Here is what I tried echo implode("\t", array_column($row, 'Product')) . "\n"; I have looked through the array functions page in the PHP manual but don't know which function is right for my requirements.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.