php数据还原

源数据:
{ "openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ", "courseID": "EC A7 D5 95", "courseName": "tiandizhijian", "studentCount": "2", "parts": [{ "actions": [{ "typeID": "30" }, { "typeID": "30" } ], "singleTime": "30", "restTime": "60" }, { "actions": [{ "typeID": "30" }, { "typeID": "30" } ], "singleTime": "30", "restTime": "60" } ] }

 存入数据库示意 

uniqueCode为每节课的编码(以区分courseID重复的情景,表示唯一性),actionsIndex表示

{
"actions": [...],
"singleTime": "30",
"restTime": "60"
}
这段数据在parts中的下标,为取数据时辨别typeID的分组作个标识

 

select数据之后数据为:

[{
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "E2EFE01E18F90861C667CED170552FC3",
	"courseID": "51 5D 49 F3",
	"courseName": "\u5927\u660e",
	"studentCount": "2",
	"actionsIndex": "0",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "1"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "E2EFE01E18F90861C667CED170552FC3",
	"courseID": "51 5D 49 F3",
	"courseName": "\u5927\u660e",
	"studentCount": "2",
	"actionsIndex": "0",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "3"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "E2EFE01E18F90861C667CED170552FC3",
	"courseID": "51 5D 49 F3",
	"courseName": "\u5927\u660e",
	"studentCount": "2",
	"actionsIndex": "1",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "5"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "E2EFE01E18F90861C667CED170552FC3",
	"courseID": "51 5D 49 F3",
	"courseName": "\u5927\u660e",
	"studentCount": "2",
	"actionsIndex": "1",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "7"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "0",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "1"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "0",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "3"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "0",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "5"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "1",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "7"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "1",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "2"
}, {
	"openID": "ouGzJv7G23rRCC0Bt70e1JLM70yQ",
	"uniqueCode": "8875ED583EFCB51A3603FE22309AA57F",
	"courseID": "66 6A CC 98",
	"courseName": "\u4e1c\u6b27\u4e1c",
	"studentCount": "6",
	"actionsIndex": "1",
	"singleTime": "30",
	"restTime": "60",
	"typeID": "4"
}]

  

将这些数据变为源数据的格式返回?

$arr = array();
		foreach ($res as $key) {
			# code...
			if(!array_key_exists($key->courseid ,$arr)){
				$arr[$key->courseid] = array();
				$arr[$key->courseid]['actions'] = array();
			}
			$arr[$key->courseid]['courseID'] = $key->courseid;
			$arr[$key->courseid]['courseName'] = $key->courseName;
			$arr[$key->courseid]['usageCount'] = $key->usageCount;
			
			$a = [
				"typeID" => $key->typeID,
				"setCount" => $key->setCount,
				"countPerSet" => $key->countPerSet,
				"timeInterval" => $key->timeInterval,
			];
			array_push($arr[$key->courseid]['actions'], $a);
		}
		$res = array();
		foreach ($arr as $key => $value) {
			# code...
			array_push($res, $value);
		}
		$obj = new stdClass();
		$obj->lessons = $res;
          //$obj为返回数据

  

posted @ 2019-01-24 11:30  SingleShadow  阅读(386)  评论(0编辑  收藏  举报