0

I am looking to convert a text based DAT file into an array in PHP. Normally I would be able to read each line and explode it into an array but this file is different.

[0]
FirstRideOn=43169.5701090972
Laps=4591
LastRideOn=43224.7924173611
Name=Standard 1
Nr=1
ResetDate=0
RunningTime=2481
Runs=233
TranNr=7435191

[1]
FirstRideOn=43149.5406271644
Laps=5528
LastRideOn=43224.7616565972
Name=Standard 2
Nr=2
ResetDate=0
RunningTime=2957
Runs=292
TranNr=8377256

I was hoping to load it into an associative array.

Any feedback or suggestions are greatly appreciated!

Statik Stasis
  • 308
  • 1
  • 5
  • 16

1 Answers1

0

It's not clear from your question exactly what format you want the array to be in. So here are two alternatives. First we read the file into an array:

$input= file('somefile.dat', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);

Then we can process the array to make an associative array. Here's the first option:

$output = array();
foreach ($input as $line) {
    if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
        $entry = (int)$matches[1];
        $output[$entry] = array();
    }
    else {
        list($key, $value) = explode('=', $line);
        $output[$entry][$key] = $value;
    }
}
print_r($output);

Which generates this output:

Array
(
    [0] => Array
        (
            [FirstRideOn] => 43169.5701090972
            [Laps] => 4591
            [LastRideOn] => 43224.7924173611
            [Name] => Standard 1
            [Nr] => 1
            [ResetDate] => 0
            [RunningTime] => 2481
            [Runs] => 233
            [TranNr] => 7435191
        )

    [1] => Array
        (
            [FirstRideOn] => 43149.5406271644
            [Laps] => 5528
            [LastRideOn] => 43224.7616565972
            [Name] => Standard 2
            [Nr] => 2
            [ResetDate] => 0
            [RunningTime] => 2957
            [Runs] => 292
            [TranNr] => 8377256
        )
)

Or we can do it this way:

// alternate style
$output = array();
foreach ($input as $line) {
    if (preg_match('/^\[(\d+)\]$/', $line, $matches)) {
        $entry = (int)$matches[1];
    }
    else {
        list($key, $value) = explode('=', $line);
        if (!array_key_exists($key, $output)) $output[$key] = array();
        $output[$key][$entry] = $value;
    }
}
print_r($output);

which generates this output. The choice is yours!

Array
(
    [FirstRideOn] => Array
        (
            [0] => 43169.5701090972
            [1] => 43149.5406271644
        )

    [Laps] => Array
        (
            [0] => 4591
            [1] => 5528
        )

    [LastRideOn] => Array
        (
            [0] => 43224.7924173611
            [1] => 43224.7616565972
        )

    [Name] => Array
        (
            [0] => Standard 1
            [1] => Standard 2
        )

    [Nr] => Array
        (
            [0] => 1
            [1] => 2
        )

    [ResetDate] => Array
        (
            [0] => 0
            [1] => 0
        )

    [RunningTime] => Array
        (
            [0] => 2481
            [1] => 2957
        )

    [Runs] => Array
        (
            [0] => 233
            [1] => 292
        )

    [TranNr] => Array
        (
            [0] => 7435191
            [1] => 8377256
        )

)
Nick
  • 138,499
  • 22
  • 57
  • 95
  • Hi Nick, Thanks for your reply, the first array is output is exactly what i am after. my apologies for not making it clear. i have tested the code you provided but i am not able to get it to read. it errors out with an undefined offset 1 – danielquatz May 05 '18 at 03:35
  • I realised i made a mistake, your code works perfectly – danielquatz May 05 '18 at 03:43