Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/287.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在php中将中的字符串(以逗号分隔)转换为多维json数组_Php_Mysql_Arrays_Json - Fatal编程技术网

如何在php中将中的字符串(以逗号分隔)转换为多维json数组

如何在php中将中的字符串(以逗号分隔)转换为多维json数组,php,mysql,arrays,json,Php,Mysql,Arrays,Json,如何将数据库中带逗号的字符串转换为带名称的多维数组 我有一个列名是working hours,我们存储了数据,比如上午10:00、下午1:00、下午2:00、下午5:00 我的问题是 $sqlbranch = "SELECT * FROM branch WHERE Bank_name='$bankname'"; $resultbranch = mysql_query($sqlbranch); $branchArray = array(); if (

如何将数据库中带逗号的字符串转换为带名称的多维数组

我有一个列名是working hours,我们存储了数据,比如上午10:00、下午1:00、下午2:00、下午5:00

我的问题是

$sqlbranch = "SELECT * FROM branch WHERE 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']));
现在我想把数据转换成json数组

我希望json数据看起来像

"branches":[{"working_hours":[{"Morningtime":{"begin_time":"10:00", "end_time":"15:30"},
                        "eveningtime":{"begin_time":"16:00", "end_time":"18:30"}}]]
首先,$hours=explode',,$row['Working_hours']

接下来,将每个$hours作为$hour{$parts=explode',$hour,如果$parts[1]='pm'进行另一次分解,如$digits=explode':',$parts[0],并将12添加到$digits[0],否则使用$parts[0]作为时间,并将其存储在$cHours数组中

然后创建一个数组,比如$branchs=['working\u hours'=>['Morningtime'=>['begin\u time'=>$cHours[0],'end\u time'=>$cHours[1],'eveningtime'=>['Morningtime'=>['begin\u time'=>$cHours[2],'end\u time'=>$cHours[3]]

最后,json_对$branchs进行编码,得到json字符串