So long as the number of rows is known, it only takes:
- a regex to split the string on comma-spaces which immediately follow a one-or-more digits or a word starting with an uppercase letter,
- a call of
array_chunk()
and array_map()
(with a null callback and data spreading) to “transpose” the data.
Code: (Demo)
$string = "Service, Service, Service, 8 mars, 2017, 22 mars, 2017, 5 april, 2017, 08:00, 08:00, 08:00";
$rows = 3;
var_export(
array_map(
null,
...array_chunk(
preg_split('/(?:[A-Z][a-z]+|\d+)\K, /', $string),
$rows
)
)
);
1
solved Parse comma separated string, split array into chunks, then transpose