-1

Using the following code I am able to convert an uploaded CSV file into a php array..

    <?PHP
function readCSV($csvFile){
    $file_handle = fopen($csvFile, 'r');
    fgetcsv($file_handle);
    fgetcsv($file_handle);
    fgetcsv($file_handle);
    while (!feof($file_handle) ) {
        $line_of_text[] = fgetcsv($file_handle, 1024);
    }
    fclose($file_handle);
    return $line_of_text;
}


// Set path to CSV file
$csvFile = 'test.csv';

$csv = readCSV($csvFile);
?>

Then I can view the array on my page with..

<?php echo '<pre>';
print_r($csv);
echo '</pre>'; ?> 

This outputs the array as follows (looks like real data but has been anonymized)..

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => 
            [21] => 
            [22] => 
            [23] => 
            [24] => 2
            [25] => £3.90
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => 
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [1] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 302804801330
            [21] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.95
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
            [31] => 
            [32] => 
            [33] => 30-Jan-20
            [34] => 
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 
            [43] => 
            [44] => 
            [45] => 1711351782020
            [46] => 
            [47] => 
            [48] => 
            [49] => 
            [50] => 
            [51] => 
        )

    [2] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => 
            [4] => 
            [5] => 
            [6] => 
            [7] => 
            [8] => 
            [9] => 
            [10] => 
            [11] => 
            [12] => 
            [13] => 
            [14] => 
            [15] => 
            [16] => 
            [17] => 
            [18] => 
            [19] => 
            [20] => 302804815489
            [21] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.95
            [26] => 
            [27] => 
            [28] => 
            [29] => 
            [30] => 
            [31] => 
            [32] => 
            [33] => 30-Jan-20
            [34] => 
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 
            [43] => 
            [44] => 
            [45] => 1711351781020
            [46] => 
            [47] => 
            [48] => 
            [49] => 
            [50] => 
            [51] => 
        )

    [3] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [4] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    <!-And so on.....>

If you look at [0],[1] & [2] you will see they are all part of the same sales record number (so [0][0], [1][0] & [2][0] are the same number) (but items [3][0] & [4][0] are separate orders / different numbers). (remember though this is just an example, the data changes, there could be 100's of orders and there could be 1 or several items per order number.)

I would like to combine the arrays for orders with the same sales record number [any][0]

So something like....

if [any][0] match each other then combine the array

This would mean all the info for an order number that matches would be in 1 array per order number.

I want to keep all data (ignore blanks) but have them merged/combined into the matching top-level array as sub arrays so for example.. if [1][0] matches [2][0] all the data from [2][0] through to [2][51] (that is not empty or the same) should be added onto [1] so it becomes like...

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => 
            [21] => Array (
                [0] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
                [1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
            )
            [22] => 
            [23] => 
            [24] => Array (
                [0] => 1
                [1] => 1
            ) 
            [25] => Array (
                [0] => 1.95
                [1] => 1.95
            ) 
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => 
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [3] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [4] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    <!-And so on.....>

I hope that makes sense and that's what I think I need.

What i am trying to achieve is to make a "for each loop" for each top-level array so that I can make an invoice page, where I can then use each top-level array as a separate invoice/order page in the html markup for that page.

Glen Keybit
  • 296
  • 2
  • 15
  • It would make sense to use arrays by default, even if there's only one item for an order. – Ja͢ck Mar 16 '20 at 05:23
  • it is not too difficult, but how would you like to manage duplicate content? i don't mean, where the content is the same, but where you have the same keys with some content. for example [1][0] and [2][0] have the same id, so they belong together. now [1][20] has a value while [2][20] has another value. which one do you want to keep? or should this become an array of theese elements? – Iamnino Mar 17 '20 at 13:44
  • I have edited the OP with updated info – Glen Keybit Mar 18 '20 at 05:37

3 Answers3

2

Please try the below code. It's working.

<?php
    function readCSV($csvFile)
    {
        $line_of_text = [];
        $file_handle = fopen($csvFile, 'r');
        while (!feof($file_handle)) {
            $tmp = fgetcsv($file_handle, 1024);

            if (isset($line_of_text[$tmp[0]])) {
                foreach ($tmp as $k => $v) {

                    if (array_key_exists($k, $line_of_text[$tmp[0]])) {
                        if (!is_array($line_of_text[$tmp[0]][$k])) {
                            $kVal = $line_of_text[$tmp[0]][$k];

                            $line_of_text[$tmp[0]][$k] = [];
                            $line_of_text[$tmp[0]][$k][] = $kVal;
                        }

                        $line_of_text[$tmp[0]][$k][] = $v;
                        $line_of_text[$tmp[0]][$k] = array_unique($line_of_text[$tmp[0]][$k]);
                        $line_of_text[$tmp[0]][$k] = array_filter($line_of_text[$tmp[0]][$k]);

                        if (count($line_of_text[$tmp[0]][$k]) == 1) {
                            $line_of_text[$tmp[0]][$k] = array_values($line_of_text[$tmp[0]][$k]);
                            $line_of_text[$tmp[0]][$k] = $line_of_text[$tmp[0]][$k][0];
                        }

                        if (empty($line_of_text[$tmp[0]][$k])) {
                            $line_of_text[$tmp[0]][$k] = null;
                        }

                    } else {
                        $line_of_text[$tmp[0]][$k] = null;
                    }
                }
                $line_of_text[$tmp[0]][0] = $tmp[0];

            } else {
                $line_of_text[$tmp[0]] = $tmp;
            }

        }

        fclose($file_handle);
        return array_filter(array_values($line_of_text));
    }

    // Set path to CSV file
    $csvFile = 'test.csv';
    $csv = readCSV($csvFile);

    echo "<pre>";
    print_r($csv);

?>

=> Output

Array
(
    [0] => Array
        (
            [0] => 15304
            [1] => 15-04478-27247
            [2] => ersmith0
            [3] => eric smith
            [4] => myemail@gmail.com
            [5] => 
            [6] => 2 Any Close
            [7] => 
            [8] => Manchester
            [9] => Greater Manchester
            [10] => M1 5BB
            [11] => United Kingdom
            [12] => eric smith
            [13] => +44 7719 123456
            [14] => 27 Any Close
            [15] => 
            [16] => Manchester
            [17] => Greater Manchester
            [18] => M1 5BB
            [19] => United Kingdom
            [20] => Array
                (
                    [0] => 302804801330
                    [1] => 302804815489
                )

            [21] => Array
                (
                    [0] => CAUTION DRIVEN BY A BIKER  - Fun Vinyl Decal Sticker 4x4/Car/Lorry/Van/Camper
                    [1] => Warning Biker With an Attitude Annoy At Your Own Risk Car Vinyl Decal Sticker
                )

            [22] => 
            [23] => No
            [24] => Array
                (
                    [0] => 2
                    [1] => 1
                )

            [25] => Array
                (
                    [0] => £3.90
                    [1] => £1.95
                )

            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £3.90
            [31] => No
            [32] => PayPal
            [33] => 30-Jan-20
            [34] => 30-Jan-20
            [35] => 
            [36] => 
            [37] => 
            [38] => 31-Jan-20
            [39] => No
            [40] => 
            [41] => 
            [42] => 5LD0123456789U
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => Array
                (
                    [0] => 1711351782020
                    [1] => 1711351781020
                )

            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [1] => Array
        (
            [0] => 15305
            [1] => 25-04445-36722
            [2] => pod69
            [3] => mart badger
            [4] => martsemailddr34@gmail.com
            [5] => 
            [6] => Via Riccione N. 35/30
            [7] => 
            [8] => Sa Tiacca Quartu S. Elena
            [9] => CA
            [10] => 09445
            [11] => Italy
            [12] => Badger Mart
            [13] => +39 987 654 3211
            [14] => Via Riccione N. 35/30
            [15] => 
            [16] => Sa Tiacca Quartu S. Elena
            [17] => CA
            [18] => 09445
            [19] => Italy
            [20] => 302667176336
            [21] => 2x Funny Car & Van or Camper WV Eyelash Vinyl Sticker Decal, Signs - Any Colour!
            [22] => 
            [23] => No
            [24] => 2
            [25] => £1.99
            [26] => £3.14
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £7.12
            [31] => No
            [32] => PayPal
            [33] => 23-Jan-20
            [34] => 23-Jan-20
            [35] => 24-Jan-20
            [36] => 
            [37] => 
            [38] => 24-Jan-20
            [39] => Yes
            [40] => 
            [41] => 
            [42] => 25C157351T6888860U
            [43] => Royal Mail International Standard
            [44] => 
            [45] => 1709118816020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

    [2] => Array
        (
            [0] => 15306
            [1] => 20-04482-41274
            [2] => wideboiy38887
            [3] => Paul Lastname
            [4] => wideboiysemail@hotmail.co.uk
            [5] => Yellow please
            [6] => 10
            [7] => Broomstick Lane
            [8] => Okehampton
            [9] => Devon
            [10] => EX19 2FT
            [11] => United Kingdom
            [12] => Paul Lastname
            [13] => +44 7866 401983
            [14] => 105
            [15] => Broomstick Lane
            [16] => Okehampton
            [17] => Devon
            [18] => EX19 2FT
            [19] => United Kingdom
            [20] => 302755568952
            [21] => Junglist Soldier Car Window Laptop Wall Gift Drum Bass dnb Jungle Vinyl Sticker
            [22] => 
            [23] => No
            [24] => 1
            [25] => £1.99
            [26] => £0.00
            [27] => 
            [28] => £0.00
            [29] => £0.00
            [30] => £1.99
            [31] => No
            [32] => PayPal
            [33] => 01-Feb-20
            [34] => 01-Feb-20
            [35] => 03-Feb-20
            [36] => 
            [37] => 
            [38] => 
            [39] => No
            [40] => 
            [41] => 
            [42] => 7SD68673EE789782X
            [43] => Royal Mail 1st Class
            [44] => 
            [45] => 1711667179020
            [46] => 
            [47] => No
            [48] => 
            [49] => No
            [50] => 
            [51] => No
        )

)
Mukesh Singh Thakur
  • 1,335
  • 2
  • 10
  • 23
  • Thank you so much for your example code, I have had to heavily edit and reword my original post as I realised I'd not asked it in a manner that fully explained my needs and that I would need to add in the full example data (not just a small sample as before) in order that my problem gets properly resolved. Please re-read the first post as I have hopefully now made what i'm trying to achieve more clear and also see my updated example arrays with complete example data now included. Thanks again. – Glen Keybit Mar 17 '20 at 06:59
  • I have updated the code. Please try it. It's working now. – Mukesh Singh Thakur Mar 18 '20 at 09:11
  • When I try this code the information from [21] is missing for those that it combines? – Glen Keybit Mar 18 '20 at 15:51
  • 1
    Hi @Glen Keybit, I have updated my code for [21] issue. Please check it. – Mukesh Singh Thakur Mar 19 '20 at 05:25
  • Hi @Mukesh Singh, this is almost perfect now, but i've come across an issue I didn't realise I would have till trying it out. Could it be modified so that even if an order only has 1 item that the detail for numbers [21][24]&[25] are displayed in an array? Visual of what I'm after here.. https://imgur.com/a/DluSS5v – Glen Keybit Mar 19 '20 at 19:36
  • and [46] sorry! – Glen Keybit Mar 19 '20 at 19:47
  • I have added output with my answer. The output base on your array. If find multiple values then the data display in an array or if it is a single value than display as string. – Mukesh Singh Thakur Mar 20 '20 at 04:12
  • @ Mukesh Singh: This may well be the better answer but with my very limited knowledge I could not figure out how to use it in the way I had desired but was able with the other answer posted by Rudra Thakkar. Many thanks for all of your help. – Glen Keybit Mar 21 '20 at 09:18
  • Welcome, My answer is matched with your question's output. I checked another's answered but it's different from your question's output. Please put your output properly on your question so, we can provide a proper answer which is helpful to you. – Mukesh Singh Thakur Mar 23 '20 at 06:14
1

You can process array like this

$arr = [];
//$csv is your array
foreach($csv as $key => $value){
  $arr[$value[0]][] = $value;  
}
print_r($arr);

this code will append all nested arrays with same [0]th index.

then you can use other php functions to make your array unique.

if you want a result with merged arrays you can try below code.

$arr = [];
//$csv is your array
foreach($csv as $key => $value){
  if(!array_key_exists($value[0],$arr)){
    $arr[$value[0]] = [];
  }
  $arr[$value[0]] = array_merge($arr[$value[0]],$value);  
}
print_r($arr);

I have not tested code so please correct any error if there is.

Rudra
  • 704
  • 8
  • 16
  • 1
    This looks like a possibly good way to go about solving my issue however if I go down that route, given that the order numbers will change each time I run this how will I target each order in the for each loop? Say I wanted to echo/print_r just the info from [51234][0][12] but next time its [12345][0][12] Can I echo using a wildcard like [?][0][12] ?? – Glen Keybit Mar 18 '20 at 15:45
  • @GlenKeybit Sorry, Didn't got you, can you explain more specifically? – Rudra Mar 19 '20 at 12:14
  • @GlenKeybit I have updated answer check if it helps. – Rudra Mar 19 '20 at 12:20
  • 1
    @ Rudra: yours is the answer I have used and the answer I have awarded the bounty to because it was the only one that showed an example of how to merge the arrays in continuous numbers as I'd mentioned in my OP. Many thanks for your help. – Glen Keybit Mar 21 '20 at 09:14
  • @GlenKeybit most welcome. sorry for delayed responses. i was away from my lappy. – Rudra Mar 23 '20 at 05:49
0

I think this might result in something different than you like if the following situation can happen:

Let's say we have 3 records like this:

$original = [0] => [
  [0] => 'id0',
  [1] => 'name 0',
  [2] => 'street 0'
],
[1] => [
  [0] => 'id0',
  [1] => 'name 1',
  [2] => 
],
[2] => [
  [0] => 'id0',
  [1] =>,
  [2] => 'street 2'
]

you will end up with the following structure if i follow what i think you mean:

$new = [0] => [
  [0] => 'id0',
  [1] => [
    [0] => 'name 0',
    [1] => 'name 1'
  ],
  [2] => [
    [0] => 'street 0',
    [1] => 'street 2'
  ]
]

can you see that $new[0][1][1] is from $original[1][1], while at the same position in the array $new[0][2][1] comes form $original[2][2]

I think the problem is that you now align $original[X][1] with $original[X][2], if you print that in some sort of table / overview those end up next to each other but are not related.

==

As for how to solve your problem, I would suggest something like this:

function readCsv($csvFileName) {
    $file_handle = fopen($csvFile, 'r');
    $records = [];
    fgetcsv($file_handle);
    while (!feof($file_handle) ) {
        $line = fgetcsv($file_handle, 1024);
        $id = $line[0];
        if (!$records[$id]) $records[$id] = [];
        $records[$id][] = $line;
    }
    fclose($file_handle);
    return $records;
}

function printCsvColumn($rowName, $sales) {
    $uniqueSales = array_unique($sales);
    $uniqueNotNullSales = array_filter($uniqueSales, 'strlen');
    foreach ($uniqueNotNullSales as $sale) {
        echo $rowName . ': ' . $sale[$rowName];
    }
}

function printCsv($records) {
    foreach ($records as $sales) {
        foreach ($sales[0] as $rowName=>$rowValue) {
            printCsvColumn($rowName, $sales);
        }
    }
}

If you know OOP you can make classes and objects to represent this logic for you so you wont end up with this functional getting and iterating the data all over your code.

Paul Scheltema
  • 1,993
  • 15
  • 25
  • If the sales record number [any][0] matches [any][0] then the name is omitted anyway for any additional items ordered. i did also say in my update OP that they should only be merged into new sub arrays if the data in the matching sub array numbers is differant and if the matching sub array data number is also not blank – Glen Keybit Mar 18 '20 at 10:56
  • added the filtering @GlenKeybit – Paul Scheltema Mar 18 '20 at 11:05
  • This code just rendered a blank white page when I tried it. – Glen Keybit Mar 18 '20 at 16:04