일부 탭 구분 데이터를 사용할 수있는 더 나은 형식으로 변경하려고합니다. JSON은 훌륭 할 것이다.JSON에 탭으로 구분 된 데이터
데이터의 예는 여기에 있습니다 : http://www.nlaa.ca/results/rr/2013/20130728tely10results.php
나는 모든 데이터를 다운로드하고 그것의 자신의 텍스트 파일에서 매년를했습니다. 거기에서 나는 그들을 묶어서 foreach에 넣고 줄을 따라 읽습니다. 그래서 같이 :이 그렇다고 작동
// All the fields
$position = $clean[1];
$name = $clean[3] . " " . $clean[4];
$time = $clean[5];
$class = $clean[6];
$class_place = $clean[7];
$runners['position'] = $position;
$runners['name'] = $name;
$runners['time'] = $time;
$runners['class'] = $class;
$runners['class place'] = $class_place;
을하지만, 그것은 단지가 .txt 파일 각각에 대해 하나 개의 배열을 만들고 여기에서
// Grab all the .txt files
foreach (glob('src-data/*.txt') as $filename) {
$handle = fopen($filename, 'r');
if ($handle) {
// run line by line through the files
while (($line = fgets($handle)) !== false) {
// grab the fields
$clean = preg_split("/ +/", $line);
, 나는 거대한 연관 배열에있는 모든 라인을 데려 가고 싶다는 나는 가지고있다.
내가 print_r($runners);
일 때 나는 그 디렉토리에있는 두 개의 샘플 .txt 파일 각각에 대해 하나씩 두 개의 배열을 얻습니다.
반복 할 때 각 새 줄을 내 $runners[]
배열로 푸시합니다.
나의 궁극적 인 목표는 Js와 함께 사용할 수있는 큰 json 파일을 만드는 것입니다. 난 $runners
일할 수 있다면 그냥 json_encode($runners)
을 실행하는 것 같아요,하지만 어떤 제안을 주셔서 감사합니다. 감사!
<?php
$runners = [];
// Grab all the .txt files
foreach (glob('src-data/*.txt') as $filename) {
$handle = fopen($filename, 'r');
if ($handle) {
// run line by line through the files
while (($line = fgets($handle)) !== false) {
// grab the fields
$clean = preg_split("/ +/", $line);
// All the fields
$position = $clean[1];
$name = $clean[3] . " " . $clean[4];
$time = $clean[5];
$class = $clean[6];
$class_place = $clean[7];
$runners['position'] = $position;
$runners['name'] = $name;
$runners['time'] = $time;
$runners['class'] = $class;
$runners['class place'] = $class_place;
$runners[] = $clean;
}
print_r($runners);
// write to json file
$jsonwrite = fopen('runners.json', 'w');
fwrite($jsonwrite, json_encode($runners));
fclose($jsonwrite);
fclose($handle);
} else {
echo "Error opening file";
}
}
?>
대신 fgetcsv()를 사용하지 왜 수동으로 원시 데이터 행을 구문 분석 하시겠습니까? 특정 맞춤 분리 기호를 매우 쉽게 사용할 수 있습니다. –
Marc에게 감사합니다. 나는 이것을 빨리 시도했지만 멀리 가지 않았다. 모든 열에는 서로 다른 수의 공백이 있습니다. 아마도 fgetcsv()가 어떻게 든 그것을 처리 할 수 있습니다. 나는 google! 감사합니다 – saltcod