0

Dear Friends,

I have a string with structure

$selectedTime = "2017-02-10 07:00,2017-02-10 08:00,2017-02-11 09:00";

I need to convert it as

  {"2017-02-10":[{"start":"07:00"},{"start":"08:00"}],"2017-02-11":[{"start":"09:00"}]}

any idea ? please advise asap

Thanks
Anes

1
Contributor
1
Reply
15
Views
4 Months
Discussion Span
Last Post by logicslab
2

Dear Friends,

With the Help of my Friend Vishnu A.R I solved the issue

my required Code is

<?php
$selectedTime = "2017-02-10 08:00,2017-02-10 07:00,2017-02-11 09:00";

$arr1 = explode(",", $selectedTime);

$arr2 = array();

foreach($arr1 as $time){
  $strtime = strtotime($time);
  $arr2[$strtime] = $time;
}
ksort($arr2); // sorting array in ascending order of key value
$arr3 = array();
foreach($arr2 as $key => $time){
  $timesplit = explode(" ", $time);

  $arr3[$timesplit[0]][] = array('start' => $timesplit[1]);
}
$output = json_encode($arr3);
var_dump($output);
?>

Thanks

Anes

Edited by logicslab

Votes + Comments
Here I thought a regular expression may do. This works too.
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.