具有以下格式的文本文件:
(400, 530); 6.9; 5.7; 5.0;//------> continues for 100 values.
(500, 530); 7.9; 5.1; 5.0;
(600, 530); 6.7; 6.7; 7.2;
法典:
<?php
$file="./Speed10.asc";
$document=file_get_contents($file);
$rows = explode ('(', $document); //splits document into rows
foreach ($rows as &$rowvalue) {
explode (';', $rowvalue);<----- How to assign each of these to member
of an array??
}
}
?>
我正在尝试创建 2D 数组,首先拆分为行,然后按元素拆分为";">
示例输入:
$document='(400, 530); 6.9; 5.7; 5.0; ...
(500, 530); 7.9; 5.1; 5.0; ...
(600, 530); 6.7; 6.7; 7.2; ...';
方法 #1(数组中存储的不带分号的值(:
foreach(explode("rn",$document) as $row){ // split the content by return then newline
$result[]=explode("; ",$row); // split each row by semi-colon then space
}
var_export($result);
/* Output:
[
['(400, 530)','6.9','5.7','5.0','...'],
['(500, 530)','7.9','5.1','5.0','...'],
['(600, 530)','6.7','6.7','7.2','...']
]
) */
方法 #2(带有分号的值存储在数组中(:
foreach(explode("rn",$document) as $row){ // split the content by return then newline
$result[]=preg_split('/(?<!,) /',$row); // split each row by space not preceeded by comma
}
var_export($result);
/* Output:
[
['(400, 530);','6.9;','5.7;','5.0;','...'],
['(500, 530);','7.9;','5.1;','5.0;','...'],
['(600, 530);','6.7;','6.7;','7.2;','...']
]
) */
这是这两种方法的演示。
请记住,我只关注循环内的字符串拆分。 克里斯关于文件处理的建议是可取的。
根据您的环境,您可能需要通过移除r
或类似物来调整第一次爆炸。