Меньше реализации прерывания управления на самом деле, потому что работа с массивами делает обычные проверки и оглядываясь на значение ключа «предыдущая запись» излишним.
Вот как я бы это сделал,
<?php
// using a data array here for illustration purposes,
// replace with reading from CSV on your own
$data = array (
0 =>
array (
0 => '019',
1 => '07:50:00',
2 => '0,0',
),
1 =>
array (
0 => '017',
1 => '07:50:00',
2 => '0,8',
),
2 =>
array (
0 => '019',
1 => '07:55:00',
2 => '0,4',
),
3 =>
array (
0 => '017',
1 => '07:55:00',
2 => '1,3',
),
4 =>
array (
0 => '019',
1 => '08:00:00',
2 => '0,8',
),
5 =>
array (
0 => '017',
1 => '08:00:00',
2 => '1,9',
),
);
// initialize object and properties to hold final result
$result = new stdClass();
$result->xAxis = new stdClass();
$result->series = [];
$categories = $series = [];
// accumulate data for categories and series
// using the name as key for the series array, makes it easier - otherwise you’d
// have to check if an array entry with specific name property value already exists
foreach($data as $row) {
$categories[] = $row[1];
$series[$row[0]][] = $row[2];
}
// make categories unique to filter out duplicates, and re-index numerically
$result->xAxis->categories = array_values(array_unique($categories));
// transform temporary series data into final structure, putting the key into the
// name property now
foreach($series as $key => $item) {
$temp = new stdClass();
$temp->name = $key;
$temp->data = $item;
$result->series[] = $temp;
}
echo json_encode($result);
(Преобразование значений x,y
в последнем столбце CSV в x.y
целочисленный формат я также оставлю на ваше усмотрение.)