請問一下
我有一個CSV(ALR.csv)
time,ALR
01:01:00,-0.0153
01:01:00,-0.0247
01:01:00,-0.0239
01:01:00,-0.6721
01:01:01,-1.0153
01:01:01,-0.9247
01:01:01,-0.1239
01:01:02,-0.6153
01:01:02,-0.9247
01:01:02,-0.1239
01:01:02,-0.7210
我用fgetcsv讀出第一列time及底下所有時間的值並切割成陣列形式
$file = fopen(ALR.csv,"r");
while($row = fgetcsv($file)){
$col = $row[0];
$col = explode(',',$col);
print_r ($col);
}
輸出為
Array ( [0] => time ) Array ( [0] => 01:01:00 ) Array ( [0] => 01:01:00 )Array ( [0] => 01:01:00 ) Array ( [0] => 01:01:00 ) Array ( [0] => 01:01:01 ) Array ( [0] => 01:01:01 ) Array ( [0] => 01:01:01 ) Array ( [0] => 01:01:02 ) Array ( [0] => 01:01:02 ) Array ( [0] => 01:01:02 ) Array ( [0] => 01:01:02 )
但我一直無法改變成
Array ( [0] => time ) Array ( [1] => 01:01:00 ) Array ( [2] => 01:01:00 )Array ( [3] => 01:01:00 ) Array ( [4] => 01:01:00 ) Array ( [5] => 01:01:01 ) Array ( [6] => 01:01:01 ) Array ( [7] => 01:01:01 ) Array ( [8] => 01:01:02 ) Array ( [9] => 01:01:02 ) Array ( [10] => 01:01:02 ) Array ( [11] => 01:01:02 )
該如何編寫呢?
另
我想要01:01:01第一個出現的陣列單元:5
我可以怎麼讓它取出5呢
這樣應該比較好
$file = array_map("str_getcsv",file("ALR.csv"));
print_r($file);
result
Array
(
[0] => Array
(
[0] => time
[1] => ALR
)
[1] => Array
(
[0] => 01:01:00
[1] => -0.0153
)
[2] => Array
(
[0] => 01:01:00
[1] => -0.0247
)
[3] => Array
(
[0] => 01:01:00
[1] => -0.0239
)
[4] => Array
(
[0] => 01:01:00
[1] => -0.6721
)
[5] => Array
(
[0] => 01:01:01
[1] => -1.0153
)
[6] => Array
(
[0] => 01:01:01
[1] => -0.9247
)
[7] => Array
(
[0] => 01:01:01
[1] => -0.1239
)
[8] => Array
(
[0] => 01:01:02
[1] => -0.6153
)
[9] => Array
(
[0] => 01:01:02
[1] => -0.9247
)
[10] => Array
(
[0] => 01:01:02
[1] => -0.1239
)
[11] => Array
(
[0] => 01:01:02
[1] => -0.7210
)
)
你用 fgetcsv
index 都會是 0 的原因應該是
fgetcsv
每次只讀一行
所以你想用你原本的方式的話
可以讀一行
就 push 到某個 array 之類的
感謝大大幫忙
我改了另一種方式
$file = fopen(ALR.csv,"r");
while($row = fgetcsv($file)){
$col = $col.$row[0].',';
}
存成test.php後再另一個php帶入$col,即達到
Array ( [0] => time ) Array ( [1] => 01:01:00 ) Array ( [2] => 01:01:00 )Array ( [3] => 01:01:00 ) Array ( [4] => 01:01:00 ) Array ( [5] => 01:01:01 ) Array ( [6] => 01:01:01 ) Array ( [7] => 01:01:01 ) Array ( [8] => 01:01:02 ) Array ( [9] => 01:01:02 ) Array ( [10] => 01:01:02 ) Array ( [11] => 01:01:02 )
那我想要01:01:01最後一個出現的陣列單元:7
我可以怎麼讓它取出7呢?
感謝dragonH