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 代码,我试图将每一行存储在具有五个索引的数组中(行被逗号分解):

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

其中$lines包含记录..

但问题是,它显示:

    Array
(
    [0] => 

)
Array
(
    [0] =>  

)
Array
(
    [0] =>     

)
Array
(
    [0] => 

)
Array
(
    [0] => 

)
Array
(
    [0] => 
)
Array
(
    [0] => 2013-01-19 03:00:00
    [1] => 1.33019
    [2] => 1.33142
    [3] => 1.33019
    [4] => 1.33081
    [5] => 637

)
Array
(
    [0] => 2013-01-19 03:15:00
    [1] => 1.33083
    [2] => 1.33129
    [3] => 1.33069
    [4] => 1.33095
    [5] => 425

)

......

我不知道零(空数组)来自哪里。

代码有问题吗?谢谢您的帮助..

4

2 回答 2

2

删除第一个@而不是爆炸它:

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

}

爆炸会在您的数组@中产生一个空值-请参见此处$data

于 2013-01-22T10:36:30.350 回答
0

删除 $data = explode('@', $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>';
        }
    }
于 2013-01-22T10:38:36.353 回答