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.
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.