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