第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

使用鍵從數(shù)組中獲取第一個元素

使用鍵從數(shù)組中獲取第一個元素

PHP
開滿天機 2022-10-28 15:38:17
我對 PHP(來自 JS)很陌生,所以在調(diào)試這個問題時遇到了麻煩。我已導入 a.csv以呈現(xiàn)到 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")?>// .csvDate,TransactionNumber,CustomerNumber,Reference,Amount2016-12-10 1:54PM,NUF5V6PT3U,5156,Purchase at JB HiFi,-24982016-12-4 4:54AM,J82964EFPS,2347,Refund,54242016-12-12 5:25PM,ZUFBQGCKTK,5683,Fee Refund,2542016-12-01 12:00AM,XHNCASYLBR,2347,Purchase at Coles,-88732016-11-23 10:34PM,S98EBHDWG3,3423,Wages,1987002017-09-23 9:34AM,MPNQYKVJ3G,4657,Purchase at Chemist Warehouse,-5842015-11-23 10:34PM,74CQKEGSHB,2173,Refund,-35142015-11-23 10:35PM,WPTJMNVH4U,4527,Purchase at Hungry Monkey,452452017-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        )
查看完整描述

2 回答

?
慕森卡

TA貢獻1806條經(jīng)驗 獲得超8個贊

您的 CSV 文件可能使用 UTF-8 編碼,帶有BOM(字節(jié)順序標記)。

PHP 在讀取文件時確實(仍然)不能正確處理 BOM,因此您從文件中讀取的第一行仍然會在開始時包含它,因此第一列值也仍然會包含它。因此,您在$csv數(shù)組中的鍵實際上不是Date,而是[BOM]Date,但是您在 print_r 輸出中看不到它(BOM 本身實際上并不“可見”。)

有關(guān)如何刪除 BOM 的建議,請參閱如何刪除多個 UTF-8 BOM 序列https://gist.github.com/chrisguitarguy/6096271。


查看完整回答
反對 回復 2022-10-28
?
烙印99

TA貢獻1829條經(jīng)驗 獲得超13個贊

您需要獲取每個字段的交易。這里:https ://www.php.net/manual/en/function.fgetcsv.php


我正在使用完全相同的 CSV 文件并為面試制定了解決方案??梢栽谝韵骆溄由蠝y試 CSV 文件: 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; ?>

截圖如下:

http://img1.sycdn.imooc.com//635b871a0001731909890970.jpg

查看完整回答
反對 回復 2022-10-28
  • 2 回答
  • 0 關(guān)注
  • 103 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號