使用键从数组中获取第一个元素

我对 PHP(来自 JS)很陌生,所以在调试这个问题时遇到了麻烦。

我已导入 a.csv以呈现到 html 表,并试图检索一列,以便我可以订购表。该表包含 5 个标题(日期、交易编号、客户编号、参考、金额)。我可以检索除Date列之外的所有值。那只是返回一个空值。


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

        )


开满天机
浏览 84回答 2
2回答

慕森卡

您的 CSV 文件可能使用 UTF-8 编码,带有BOM(字节顺序标记)。PHP 在读取文件时确实(仍然)不能正确处理 BOM,因此您从文件中读取的第一行仍然会在开始时包含它,因此第一列值也仍然会包含它。因此,您在$csv数组中的键实际上不是Date,而是[BOM]Date,但是您在 print_r 输出中看不到它(BOM 本身实际上并不“可见”。)有关如何删除 BOM 的建议,请参阅如何删除多个 UTF-8 BOM 序列或https://gist.github.com/chrisguitarguy/6096271。

烙印99

您需要获取每个字段的交易。这里:https ://www.php.net/manual/en/function.fgetcsv.php我正在使用完全相同的 CSV 文件并为面试制定了解决方案。可以在以下链接上测试 CSV 文件: https ://paduademo.azurewebsites.net/<?php foreach ($transactions as $transaction) : ?>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $transaction['Date']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $transaction['TransactionNumber']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo ($transaction['Valid'])? 'Yes': 'No'; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $transaction['CustomerNumber']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><?php echo $transaction['Reference']; ?></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; <td><span class="<?php echo ($transaction['type'] == 'Credit')? 'text-danger': 'text-success'; ?>"><?php echo (($transaction['type'] == 'Credit')? '-': '') . '$' . $transaction['Amount']; ?></span></td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </tr><?php endforeach; ?>截图如下:
打开App,查看更多内容
随时随地看视频慕课网APP