php将多维数据保存进json文件

/**
     * @description 将任务信息保存到json文件中
     *
     * @param string $filename
     *
     * @return bool
     * @throws MYS_RuntimeEvent
     */
    public function saveToJson($filename) {
        /**
         * 1. JSON数据的格式如下
         *"RestoreOrder": {
         *    "timeStamp": "2016-12-28 14:03:50",
         *    "isChecked":"",
         *    "dataSaved":"",
         *    "dataSure":"",
         *    "hostIp":"",
         *    "hostUUID":""
         *    "inst": {
         *        "inst1": {
         *            "db1": {
         *                "dbname":""
         *                "isRestore":"",
         *                "inst_name": "",
         *                "inst_port": "",
         *                "des_port": "",
         *                "des_path": "",
         *                "des_connect": {
         *                      "user" : "",
         *                      "key" : "",
         *                      "connect_ip" : "",
         *                  }
         *            },
         *            "db2": {...}
         *        },
         *        "inst2": {...}
         *        }
         *    }
         *};
         * 2. 有些数据需要从数据库中查询获得
         * 
         * 3.$task的数据
         * $temp_data = array(
         *  "dbname" => $this->timestamp,
         *  "isRestore" => $this->ischecked,
         *  "inst_name" => $this->datasaved,
         *  "inst_port" => $this->datasure,
         *  "des_port" => $this->hostip,
         *  "des_path" => $this->uuid
        );

         */
        if (!$this->OrderValid) {
            throw new MYS_RuntimeEvent(MYS_Severity::SEVERITY_EMERGENCY,
                OBJECT_PARAM_INVALID, __CLASS__ . ":" . __FUNCTION__, __LINE__);
        }

        // 创建json数据
        $temp_data = array(
            "timeStamp" => $this->timestamp,
            "isChecked" => $this->ischecked,
            "dataSaved" => $this->datasaved,
            "dataSure" => $this->datasure,
            "hostIp" => $this->hostip,
            "hostUUID" => $this->uuid
        );

        // 创建恢复任务对象信息
        foreach ($this->tasklist as $task) {
            $instconnect = $task['des_connect'];
            $arr_auth = $instconnect->getAuthenticationArr();
            $arr_conn = $instconnect->getConnectionArr();
            unset($task['isRestore']);
            unset($task['instconnect']);
            $temp_data['inst'][$task['inst_name']][$task['dbname']] = $task;
            $temp_data['inst'][$task['inst_name']][$task['dbname']]['des_connect'] =
                array(
                    "user" => $arr_auth['UID'],
                    "key" => $arr_auth['PWD'],
                    "connect_ip" => $arr_conn['HOST'],
                );
            $temp_data['inst'][$task['inst_name']][$task['dbname']]['isRestore'] =
                $this->isrestore[$task['inst_name']][$task['dbname']];
        }

        // 保存到文件中
        file_put_contents(dirname(__FILE__) . "/../" . MYSQLTaskRSController::TMPTASKSHADOWRS_LOCK, json_encode($temp_data));

        copy(dirname(__FILE__) . "/../" . MYSQLTaskRSController::TMPTASKSHADOWRS_LOCK, $filename);
    }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值