2

I am very new to PHP (coming from JS) so am having trouble debugging this issue.
I have imported a .csv to render to a html table and am trying to retrieve a column so I can order the table. The table contains 5 headings (Date, TransactionNumber, CustomerNumber, Reference, Amount). I can retrieve all of the values except the Date column. That just returns an empty value.

<?php
  $rows = array_map('str_getcsv', file('BankTransactions.csv'));
  $header = array_shift($rows);
  $csv = array();
  foreach ($rows as $row) {
    $csv[] = array_combine($header, $row);
  }

  print_r($csv[0]['Date']);
  print_r("\n")
?>
// .csv
Date,TransactionNumber,CustomerNumber,Reference,Amount
2016-12-10 1:54PM,NUF5V6PT3U,5156,Purchase at JB HiFi,-2498
2016-12-4 4:54AM,J82964EFPS,2347,Refund,5424
2016-12-12 5:25PM,ZUFBQGCKTK,5683,Fee Refund,254
2016-12-01 12:00AM,XHNCASYLBR,2347,Purchase at Coles,-8873
2016-11-23 10:34PM,S98EBHDWG3,3423,Wages,198700
2017-09-23 9:34AM,MPNQYKVJ3G,4657,Purchase at Chemist Warehouse,-584
2015-11-23 10:34PM,74CQKEGSHB,2173,Refund,-3514
2015-11-23 10:35PM,WPTJMNVH4U,4527,Purchase at Hungry Monkey,45245
2017-01-01 12:00AM,U6BD3M75FD,7577,Interest,2778
// Array received from `.csv`
Array
(
    [0] => Array
        (
            [Date] => 2016-12-10 1:54PM
            [TransactionNumber] => NUF5V6PT3U
            [CustomerNumber] => 5156
            [Reference] => Purchase at JB HiFi
            [Amount] => -2498
        )

    [1] => Array
        (
            [Date] => 2016-12-4 4:54AM
            [TransactionNumber] => J82964EFPS
            [CustomerNumber] => 2347
            [Reference] => Refund
            [Amount] => 5424
        )

    [2] => Array
        (
            [Date] => 2016-12-12 5:25PM
            [TransactionNumber] => ZUFBQGCKTK
            [CustomerNumber] => 5683
            [Reference] => Fee Refund
            [Amount] => 254
        )

    [3] => Array
        (
            [Date] => 2016-12-01 12:00AM
            [TransactionNumber] => XHNCASYLBR
            [CustomerNumber] => 2347
            [Reference] => Purchase at Coles
            [Amount] => -8873
        )

    [4] => Array
        (
            [Date] => 2016-11-23 10:34PM
            [TransactionNumber] => S98EBHDWG3
            [CustomerNumber] => 3423
            [Reference] => Wages
            [Amount] => 198700
        )

    [5] => Array
        (
            [Date] => 2017-09-23 9:34AM
            [TransactionNumber] => MPNQYKVJ3G
            [CustomerNumber] => 4657
            [Reference] => Purchase at Chemist Warehouse
            [Amount] => -584
        )

    [6] => Array
        (
            [Date] => 2015-11-23 10:34PM
            [TransactionNumber] => 74CQKEGSHB
            [CustomerNumber] => 2173
            [Reference] => Refund
            [Amount] => -3514
        )

    [7] => Array
        (
            [Date] => 2015-11-23 10:35PM
            [TransactionNumber] => WPTJMNVH4U
            [CustomerNumber] => 4527
            [Reference] => Purchase at Hungry Monkey
            [Amount] => 45245
        )

    [8] => Array
        (
            [Date] => 2017-01-01 12:00AM
            [TransactionNumber] => U6BD3M75FD
            [CustomerNumber] => 7577
            [Reference] => Interest
            [Amount] => 2778
        )

)

Tristan
  • 341
  • 4
  • 17
  • 3
    Strange because using your code (just copy/paste), I got `2016-12-10 1:54PM` as output. Could you please set `error_reporting(E_ALL);` in order to see is there any errors? – mitkosoft Mar 12 '20 at 09:12
  • I got also `2016-12-10 1:54PM` date as output. – Mukesh Singh Thakur Mar 12 '20 at 09:13
  • This is disappointing. Its a literal copy paste from my IDE. @mitkosoft where do I set that `error_reporting`? – Tristan Mar 12 '20 at 09:16
  • @Tristan, paste this line on the very top of your page that contains this script. Is there any other PHP code there that may cause a problem, or that's all btw? – mitkosoft Mar 12 '20 at 09:17
  • @mitkosoft Thats all I have. I've isolated this code so I can debug it without any other influences. I ran the error report and received the following `Notice: Undefined index: Date in /Users/fifmac2/apps/php/script.php on line 12` All the other keys still return a value – Tristan Mar 12 '20 at 09:20
  • Could you please add `print_r($csv);` right after `foreach` loop then and let us know the output. – mitkosoft Mar 12 '20 at 09:23
  • That returns the array above. That is how I got it. – Tristan Mar 12 '20 at 09:25
  • I have realised that it is always the first key that doesnt work. So if I change the order of the top row of the csv and put `TransactionNumber` first that returns nothing and `Date` will return a value – Tristan Mar 12 '20 at 09:27
  • 3
    Guess: Your CSV file is probably encoded in UTF-8 _with_ a BOM? PHP does not handle BOMs properly, so the very first line you read from the file will still contain it at the start, and therefor the first column value will also still contain it. Your key inside the $csv array is not actually `Date`, but `[BOM]Date`, but you don’t see it in the print_r output. (See https://stackoverflow.com/questions/10290849/ or https://gist.github.com/chrisguitarguy/6096271 for suggestions how the BOM can be removed.) – CBroe Mar 12 '20 at 09:28
  • Legend. Looks like thats what it was. PHP is confusing! – Tristan Mar 12 '20 at 10:05
  • Great. Transformed my comment into an answer, feel free to accept if it helped. – CBroe Mar 12 '20 at 10:31

2 Answers2

2

Your CSV file is probably encoded in UTF-8 with a BOM, Byte Order Mark.

PHP does (still) not handle BOMs properly when reading files, so the very first line you read from the file will still contain it at the start, and therefor the first column value will also still contain it. So your key inside the $csv array is not actually Date, but [BOM]Date, but you don’t see that in the print_r output (a BOM isn’t actually “visible” per se.)

See How to remove multiple UTF-8 BOM sequences or https://gist.github.com/chrisguitarguy/6096271 for suggestions how the BOM can be removed.

CBroe
  • 91,630
  • 14
  • 92
  • 150
0

You need to fetch the transaction for each field. Here: https://www.php.net/manual/en/function.fgetcsv.php

I am using the exact same CSV file and made a solution for an interview. The CSV file's can be tested on the below link: https://paduademo.azurewebsites.net/

<?php foreach ($transactions as $transaction) : ?>
                    <tr>
                        <td><?php echo $transaction['Date']; ?></td>
                        <td><?php echo $transaction['TransactionNumber']; ?></td>
                        <td><?php echo ($transaction['Valid'])? 'Yes': 'No'; ?></td>
                        <td><?php echo $transaction['CustomerNumber']; ?></td>
                        <td><?php echo $transaction['Reference']; ?></td>
                        <td><span class="<?php echo ($transaction['type'] == 'Credit')? 'text-danger': 'text-success'; ?>"><?php echo (($transaction['type'] == 'Credit')? '-': '') . '$' . $transaction['Amount']; ?></span></td>
                    </tr>
<?php endforeach; ?>

The screenshot is below: Using the BankTransaction.csv

Ashman Malik
  • 267
  • 3
  • 15