Answers for "return in json php"

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
15

json php

//Json Encode

$person = array( 
    "name" => "KINGASV", 
    "title" => "CTO"
); 
$personJSON=json_encode($person);//returns JSON string

//Json Decode

$personJSON = '{"name":"KINGASV","title":"CTO"}';

$person = json_decode($personJSON);

echo $person->name; // KINGASV
Posted by: Guest on July-16-2020
5

php return json

header('Content-type: application/json');
echo json_encode($array);
Posted by: Guest on March-20-2020
1

how to receive json data in php

$data = json_decode(file_get_contents('php://input'), true);
print_r($data);
echo $data;
Posted by: Guest on November-01-2020
0

php get json array

$json = '
{
    "type": "donut",
    "name": "Cake",
    "toppings": [
        { "id": "5002", "type": "Glazed" },
        { "id": "5006", "type": "Chocolate with Sprinkles" },
        { "id": "5004", "type": "Maple" }
    ]
}';

$yummy = json_decode($json, true);

echo $yummy['toppings'][2]['type']; //Maple
Posted by: Guest on February-01-2021

Browse Popular Code Answers by Language