Input string:
$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';
And I'm trying to convert it into an array that should look like this:
Array
(
[0] => Array
(
endTime => "2017-03-29T17:15:00.000+11:00"
startTime => "2017-03-29T17:00:00.000+11:00"
)
[1] => Array
(
endTime => "2017-03-31T17:15:00.000+11:00"
startTime => "2017-03-31T17:00:00.000+11:00"
)
[2] => Array
(
endTime => "2017-04-01T12:15:00.000+11:00"
startTime => "2017-04-01T12:00:00.000+11:00"
)
)
I've tried exploding, combining and all sorts but my code is so messy that I'm sure there must be a better and cleaner way?
This is MY cleanest starting point, but even this not clean, yes?
$times = '{endTime:"2017-03-29T17:15:00.000+11:00",startTime:"2017-03-29T17:00:00.000+11:00"},{endTime:"2017-03-31T17:15:00.000+11:00",startTime:"2017-03-31T17:00:00.000+11:00"},{endTime:"2017-04-01T12:15:00.000+11:00",startTime:"2017-04-01T12:00:00.000+11:00"}';
$timesarr = explode("},{", $times);
foreach ($timesarr as $i => $item) {
$timesarr[$i] = str_replace("{", "", $item);
$timesarr[$i] = str_replace("}", "", $timesarr[$i]);
$timesarr[$i] = explode(",", $timesarr[$i]);
}
echo '<pre>'; print_r($timesarr); echo '</pre>';
json_decode()