2013-01-22 67 views
1

我有這個示例記錄解析:爆炸和在陣列中存儲的值不考慮空值

@2013-01-19 02:45:00,1.32927,1.33029,1.32909,1.33018,464 
@2013-01-19 03:00:00,1.33019,1.33142,1.33019,1.33081,637 
@2013-01-19 03:15:00,1.33083,1.33129,1.33069,1.33095,425 
@2013-01-19 03:30:00,1.33092,1.3317,1.33045,1.33144,500 
@2013-01-19 03:45:00,1.33146,1.33211,1.33095,1.33196,480 
@2013-01-19 04:00:00,1.33197,1.33291,1.33166,1.33264,593 
@2013-01-19 04:15:00,1.33267,1.33308,1.33234,1.33238,366 
@2013-01-19 04:30:00,1.33239,1.33278,1.33236,1.33263,335 

使用此PHP代碼,IAM試圖每一行存儲在與5個索引的陣列(線由分解逗號):

foreach($lines as $line){ 
    $data = explode('@', $line); 
    foreach($data as $record){ 
     $val = explode(',',$record); 
     echo '<pre>'; 
     print_r($val); 
     echo '</pre>'; 
    } 
} 

其中$線包含記錄..

但問題是,它會顯示:我不知道零(空數組)來自哪裏。

代碼有問題嗎? 感謝您的幫助..

回答

2

刪除第一個@,而不是爆炸是:

foreach($lines as $line){ 
    // remove the first character 
    $record = substr($line,1); 
    $val = explode(',',$record); 
    echo '<pre>'; 
    print_r($val); 
    echo '</pre>'; 

} 

爆炸的@在您$data陣列產生空值 - see here

0

刪除$數據=爆炸('@',$ line);如果不需要或改變它你需要

$lines[] = '2013-01-19 02:45:00,1.32927,1.33029,1.32909,1.33018,464'; 
    foreach($lines as $line){ 
     $data = explode('@', $line); 
     foreach($data as $record){ 
      $val = explode(',',$record); 
      echo '<pre>'; 
      print_r($val); 
      echo '</pre>'; 
     } 
    }