mysql - How to convert string (separated by commas)in to mutidimensional json array in php -


how convert string commas database multidimensional array names

i have 1 column name working hours in stored the data 10:00 am,1:00 pm,2:00 pm,5:00 pm

my query is

$sqlbranch = "select * branch bank_name='$bankname'";         $resultbranch = mysql_query($sqlbranch);         $brancharray = array();         if (mysql_num_rows($resultbranch) > 0) {           while ($row = mysql_fetch_assoc($resultbranch)) {                $brancharray['branches'][] =array('working hours' =>$row['working_hours'])); 

now want convert data json array

i want json data looks like

"branches":[{"working_hours":[{"morningtime":{"begin_time":"10:00", "end_time":"15:30"},                         "eveningtime":{"begin_time":"16:00", "end_time":"18:30"}}]] 

first, $hours = explode(',', $row['working_hours']).

next, foreach ($hours $hour) { $parts = explode(' ', $hour) , if ($parts[1] == 'pm' ) explode $digits = explode(':', $parts[0]) , add add 12 $digits[0] else use $parts[0] time, , store in $chours array.

after create array $branches = ['working_hours' => [ 'morningtime' => ['begin_time' => $chours[0], 'end_time' => $chours[1]], 'eveningtime' => ['morningtime' => ['begin_time' => $chours[2], 'end_time' => $chours[3]]

and json_encode($branches) json string.


Comments