PHP class instance to JSON

后端 未结 5 2154
庸人自扰
庸人自扰 2020-12-08 09:28

I\'m trying echo the contents of an object in a JSON format. I\'m quite unexperienced with PHP and I was wondering if there is a predefined function to do this (like json_en

5条回答
  •  再見小時候
    2020-12-08 10:04

    public function toJSON(){
        $json = array(
            'name' => $this->getName(),
            'code' => $this->getCode(),
            'msg' => $this->getMsg(),
        );
    
        return json_encode($json);
    }
    

    Demo: http://codepad.org/mPNGD6Gv

提交回复
热议问题