saltcod saltcod - 4 months ago 22
JSON Question

Tab delimited data to JSON

I'm trying to wrangle some tab delimited data into a better format that I can work with. JSON would be just great.

Data example is here: http://www.nlaa.ca/results/rr/2013/20130728tely10results.php

I've downloaded all the data and have each year in it's own text file. From there, I'm globbing them and running them into a foreach and reading through them line by line. Like so:

// 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);


From here, I want to put all the lines in a giant associative array:

// 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;


This sorta works, but it only creates one array for each of the .txt files I have.

When I do
print_r($runners);
I get two arrays — one for each of the two sample .txt files I have in that directory.

I want to push each new line to my
$runners[]
array as I loop over it.

My ultimate goal here is to create a big json file that I can use with Js down the road. I think just running
json_encode($runners)
will work if I can get
$runners
working, but appreciate any suggestions. Thanks!

Here's the full code I'm using, just to be sure:

<?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";
}
}



?>

Answer

Replace:

$runners['position'] = $position;
$runners['name'] = $name;
$runners['time'] = $time;
$runners['class'] = $class;
$runners['class place'] = $class_place;

$runners[] = $clean;

With:

$runners[] = [
    'position' => $position,
    'name' => $name,
    'time' => $time,
    'class' => $class,
    'class_place' => $class_place
];

And you'll get something like (dummy data):

array(
    [0] => array(
        'position' => 1,
        'name' => 'Hans Muster',
        'time' => '05:18',
        'class' => '1A',
        'class_place' => '2'
    ),
    [1] => array(
        'position' => 4,
        'name' => 'Franz Muster',
        'time' => '08:12',
        'class' => '1A',
        'class_place' => '7'
    )
)