donglang7236 2015-05-06 15:35
浏览 45
已采纳

无法从数组中获取值

This is a sample array from a csv file upload, which I'm working on

$csvData = array(7) { 
               [" Account Number"]=> string(32) " 1011001562781010" 
               [" Posting Date"]=> string(10) "07/08/2014" 
               [" Value Date"]=> string(10) "07/08/2014"
               [" Description"]=> string(46) "Cash Withdrawal by Cheque173320--TT1421957901"
               [" Debit Amount"]=> string(7) "2000.00"  
               [" Credit Amount"]=> string(0) "" 
               ["Running Balance"]=> string(9) "388392.62" 
            }    

While trying to fetch value using the $csvData ,I get the following error,

Notice: Undefined index: Account Number

This is my code:

foreach ($csvData as $csvRow) {
    $csvData[$key] =$csvRow[$this->_csvColumnAccountNumber];
    if (strtolower($csvRow[$this->_csvColumnAccountNumber]) === $csv_ied_account_number) {
        $CsvValues[$key] = $csvRow[$this->_csvColumnCreditAmount];
        $CsvValues[$key] = $csvRow[$this->_csvColumnDebitAmount];
    }
}

I'm really stuck on this small issue. Please help me solve this as soon as possible.

Thanks in advance.

  • 写回答

2条回答 默认 最新

  • doushichi3678 2015-05-06 16:03
    关注

    $csvData is a single dimensional associative array. If you run foreach ($csvData as $csvRow) $csvRow is going to be each value ('1011001562781010', '07/08/2014', and so on) which is not what you want, considering you are trying to access the key which won't be available.

    So the first problem is you don't need a foreach. $csvData[' Account Number'] is what you are after.

    This brings us to the second problem. You have white space in your key. You should run trim() on the data when creating $csvData to get rid of the whitespace. Otherwise, you can do an array_walk and trim the data with an anonymous function:

    array_walk($csvData, function($value, $key) use (&$csvData) {
      unset($csvData[$key]);
      $csvData[trim($key)] = $value;
    });
    

    Above is a pretty complex example for beginners. What this does is goes through every element in $csvData and runs the callback (function) provided. This function will unset the current key and define a new trimmed key. You have to use use (&$csvData) to provide a reference to $csvData in the function's scope.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'