Advertisement
jayankaghosh

make-csv-from-dataset

May 21st, 2024
597
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.68 KB | Source Code | 0 0
  1. <?php
  2.  
  3. $csvFile = __DIR__ . '/output.csv';
  4.  
  5. $headers = [
  6.     "Partner Id",
  7.     "Partner Company Name",
  8.     "Access Point Id",
  9.     "External Store Id",
  10.     "Store City",
  11.     "Store State",
  12.     "Store Postal code",
  13.     "Line Item Type",
  14.     "Event Date",
  15.     "Event Time",
  16.     "Transaction Id",
  17.     "Package Id",
  18.     "Tracking Id",
  19.     "TimeZone",
  20.     "Store Country Code",
  21.     "Pickup Reason"
  22. ];
  23.  
  24. $dataSet = [
  25.     ["1.16589E+11", "POST OFFICE LIMITED", "3031fb74-5edc-4942-aac8-8a32a103f500", "POL-0011045", "Stanford-le-Hope", "", "SS17 0LD", "HELIX_BASE", "02.04.2024", "02.04.2024 15:36:19", "PickedUp_5ec8313f-b136-4609-a5d3-2f91a221add2", "5ec8313f-b136-4609-a5d3-2f91a221add2", "QB0599015925", "Europe/London", "GB", "PickedUp"],
  26.     ["1.16589E+11", "POST OFFICE LIMITED", "3031fb74-5edc-4942-aac8-8a32a103f500", "POL-0011045", "Stanford-le-Hope", "", "SS17 0LD", "HELIX_BASE", "02.04.2024", "02.04.2024 11:38:34", "PickedUp_7f944ffc-9ad0-4c08-a10f-342023abfe89", "7f944ffc-9ad0-4c08-a10f-342023abfe89", "QB0990618190", "Europe/London", "GB", "PickedUp"],
  27.     ["1.16589E+11", "POST OFFICE LIMITED", "3031fb74-5edc-4942-aac8-8a32a103f500", "POL-0011045", "Stanford-le-Hope", "", "SS17 0LD", "HELIX_BASE", "03.04.2024", "03.04.2024 09:39:34", "PickedUp_a7d54444-d482-4cdd-8c49-6d4c82c30642", "a7d54444-d482-4cdd-8c49-6d4c82c30642", "QB0998667340", "Europe/London", "GB", "PickedUp"],
  28.     ["1.16589E+11", "POST OFFICE LIMITED", "3031fb74-5edc-4942-aac8-8a32a103f500", "POL-0011045", "Stanford-le-Hope", "", "SS17 0LD", "HELIX_BASE", "03.04.2024", "03.04.2024 16:21:14", "PickedUp_70aac152-2c99-4d98-9767-86e91a4238eb", "70aac152-2c99-4d98-9767-86e91a4238eb", "QB0556995244", "Europe/London", "GB", "PickedUp"],
  29.     ["1.16589E+11", "POST OFFICE LIMITED", "3031fb74-5edc-4942-aac8-8a32a103f500", "POL-0011045", "Stanford-le-Hope", "", "SS17 0LD", "HELIX_BASE", "03.04.2024", "03.04.2024 13:44:19", "PickedUp_1088a1c2-de1e-440a-9c52-a9ff3ab5f4b4", "1088a1c2-de1e-440a-9c52-a9ff3ab5f4b4", "QB0560990514", "Europe/London", "GB", "PickedUp"],
  30.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "16.04.2024", "16.04.2024 17:16:51", "ReturnedByCustomer_01a4b03a-5b40-4231-abbe-5cb85ac2c5cd_1", "01a4b03a-5b40-4231-abbe-5cb85ac2c5cd", "DRhsvdRvRRMA", "Europe/London", "GB", "ReturnedByCustomer"],
  31.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "16.04.2024", "16.04.2024 17:16:51", "ReturnedByCustomer_4993ccd1-9f7c-4cde-8270-750577f33031_1", "4993ccd1-9f7c-4cde-8270-750577f33031", "DSLyxKRbRRMA", "Europe/London", "GB", "ReturnedByCustomer"],
  32.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "16.04.2024", "16.04.2024 17:16:51", "ReturnedByCustomer_4bdb47c3-fead-440a-910a-89356bc71d72_1", "4bdb47c3-fead-440a-910a-89356bc71d72", "DNxZvpRJRRMA", "Europe/London", "GB", "ReturnedByCustomer"],
  33.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "16.04.2024", "16.04.2024 17:16:51", "ReturnedByCustomer_bf712ad1-357e-44e4-bdf5-f854561ca872_1", "bf712ad1-357e-44e4-bdf5-f854561ca872", "DMxTczRWRRMA", "Europe/London", "GB", "ReturnedByCustomer"],
  34.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "16.04.2024", "16.04.2024 17:16:51", "ReturnedByCustomer_37dd6477-6fd3-4f9b-93c3-38ca68f0642a_1", "37dd6477-6fd3-4f9b-93c3-38ca68f0642a", "DbCxzrRGRRMA", "Europe/London", "GB", "ReturnedByCustomer"],
  35.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "23.04.2024", "23.04.2024 14:07:41", "ReturnedToCarrier_8eae72c2-3569-469f-ba59-03588d0979d3", "8eae72c2-3569-469f-ba59-03588d0979d3", "QC0079618257", "Europe/London", "GB", "ReturnedToCarrier"],
  36.     ["1.16589E+11", "POST OFFICE LIMITED", "09c831eb-16d6-47c1-9a69-1d36c21582a5", "POL-0012106", "Sleaford", "", "NG34 7PD", "HELIX_BASE", "23.04.2024", "23.04.2024 14:07:41", "ReturnedToCarrier_37390dbb-ca63-48d0-ad84-ca59aea95103", "37390dbb-ca63-48d0-ad84-ca59aea95103", "QC0079622691", "Europe/London", "GB", "ReturnedToCarrier"],
  37.     ["1.16589E+11", "POST OFFICE LIMITED", "dc72e7aa-fa77-4d10-b45b-0ae085c72c11", "POL-0017116", "Londonderry", "", "BT48 8JJ", "HELIX_BASE", "06.04.2024", "06.04.2024 11:48:28", "ReturnedToCarrier_e6c13f23-39cf-4c9a-981a-339862c2b471", "e6c13f23-39cf-4c9a-981a-339862c2b471", "QC0073709081", "Europe/London", "GB", "ReturnedToCarrier"],
  38.     ["1.16589E+11", "POST OFFICE LIMITED", "dc72e7aa-fa77-4d10-b45b-0ae085c72c11", "POL-0017116", "Londonderry", "", "BT48 8JJ", "HELIX_BASE", "10.04.2024", "10.04.2024 13:02:41", "ReturnedToCarrier_e753cd96-e3d4-49aa-8d0a-2899ff1e5e5c", "e753cd96-e3d4-49aa-8d0a-2899ff1e5e5c", "QC0079905126", "Europe/London", "GB", "ReturnedToCarrier"],
  39.     ["1.16589E+11", "POST OFFICE LIMITED", "dc72e7aa-fa77-4d10-b45b-0ae085c72c11", "POL-0017116", "Londonderry", "", "BT48 8JJ", "HELIX_BASE", "12.04.2024", "12.04.2024 12:10:07", "ReturnedToCarrier_205c7041-7869-4e90-83eb-9c80a2847151", "205c7041-7869-4e90-83eb-9c80a2847151", "QC0079072797", "Europe/London", "GB", "ReturnedToCarrier"],
  40.     ["1.16589E+11", "POST OFFICE LIMITED", "dc72e7aa-fa77-4d10-b45b-0ae085c72c11", "POL-0017116", "Londonderry", "", "BT48 8JJ", "HELIX_BASE", "17.04.2024", "17.04.2024 12:01:22", "ReturnedToCarrier_bdb5bf67-b668-4b82-bd8f-c39a9d918bd1", "bdb5bf67-b668-4b82-bd8f-c39a9d918bd1", "QC0074399624", "Europe/London", "GB", "ReturnedToCarrier"],
  41.     ["1.16589E+11", "POST OFFICE LIMITED", "76d6b6ff-91db-411a-ac82-ead49268550a", "POL-0019089", "Lancing", "", "BN15 0LH", "HELIX_BASE", "04.04.2024", "04.04.2024 16:29:06", "ReturnedToCarrier_290d5552-c1ee-441e-a2e4-3bb27bbf85da", "290d5552-c1ee-441e-a2e4-3bb27bbf85da", "QC0073434599", "Europe/London", "GB", "ReturnedToCarrier"]
  42. ];
  43.  
  44. function getRow($row) {
  45.     foreach ($row as &$item) {
  46.         $item = '"' . $item . '"';
  47.     }
  48.     return implode(',', $row);
  49. }
  50.  
  51. function writeRow($row, $fp)
  52. {
  53.     $row = getRow($row);
  54.     fwrite($fp, $row, strlen($row));
  55. }
  56.  
  57. if (!file_exists($csvFile)) {
  58.     $fp = fopen($csvFile, "w");
  59.     writeRow($headers, $fp);
  60.     fclose($fp);
  61.     $fp = fopen($csvFile, "a");
  62.     for ($i = 0; $i < 90; $i++) {
  63.         echo sprintf("Processing iteration %s\n", $i+1);
  64.         foreach ($dataSet as $row) {
  65.             fwrite($fp, "\n", strlen("\n"));
  66.             writeRow($row, $fp);
  67.         }
  68.     }
  69.     fclose($fp);
  70. } else {
  71.     throw new \Exception('File already exists');
  72. }
  73.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement