Answers for "php instance to json"

PHP
2

php return json

// function to return JSON response, you can set cors, also give status & data via arguments
function json($status, $data) {
        $cors = "*";
        header("Access-Control-Allow-Origin: $cors");
        header('Content-Type: application/json; charset=utf-8');
        http_response_code($status);
        echo json_encode($data);
    }

// example
json(200, [
	"status" => 200,
  	"message" => "action completed successfully",
  	"id" => 44236
]);
Posted by: Guest on October-04-2021
8

php is json string

function IsJsonString(str) {
    try {
        JSON.parse(str);
    } catch (e) {
        return false;
    }
    return true;
}
Posted by: Guest on April-10-2020
0

php object to json

function getJsonData(){
    $var = get_object_vars($this);
    foreach ($var as &$value) {
        if (is_object($value) && method_exists($value,'getJsonData')) {
            $value = $value->getJsonData();
        }
    }
    return $var;
}
Posted by: Guest on July-04-2021

Browse Popular Code Answers by Language