📜  一个完整的返回 JSON 的漂亮而清晰的 PHP 代码 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:26.596000             🧑  作者: Mango

代码示例1
$option = $_GET['option'];

if ( $option == 1 ) {
    $data = [ 'a', 'b', 'c' ];
    // will encode to JSON array: ["a","b","c"]
    // accessed as example in JavaScript like: result[1] (returns "b")
} else {
    $data = [ 'name' => 'God', 'age' => -1 ];
    // will encode to JSON object: {"name":"God","age":-1}  
    // accessed as example in JavaScript like: result.name or result['name'] (returns "God")
}

header('Content-type: application/json');
echo json_encode( $data );