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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

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

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

PHP
開(kāi)滿天機(jī) 2022-10-28 15:38:17
我對(duì) PHP(來(lái)自 JS)很陌生,所以在調(diào)試這個(gè)問(wèn)題時(shí)遇到了麻煩。我已導(dǎo)入 a.csv以呈現(xiàn)到 html 表,并試圖檢索一列,以便我可以訂購(gòu)表。該表包含 5 個(gè)標(biāo)題(日期、交易編號(hào)、客戶編號(hào)、參考、金額)。我可以檢索除Date列之外的所有值。那只是返回一個(gè)空值。<?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貢獻(xiàn)1806條經(jīng)驗(yàn) 獲得超8個(gè)贊

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

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

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


查看完整回答
反對(duì) 回復(fù) 2022-10-28
?
烙印99

TA貢獻(xiàn)1829條經(jīng)驗(yàn) 獲得超13個(gè)贊

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


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

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

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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