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

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

commented: Here I thought a regular expression may do. This works too. +15
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.