我要解析此示例记录:
@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尝试将每行存储在一个包含五个索引的数组中(行以逗号分隔):
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
)
......
我不知道零(空数组)的来源。
代码有问题吗? 谢谢你的帮助......
答案 0 :(得分: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
答案 1 :(得分: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>';
}
}