Pretty-Printing JSON with PHP

后端 未结 24 2172
一向
一向 2020-11-22 02:03

I\'m building a PHP script that feeds JSON data to another script. My script builds data into a large associative array, and then outputs the data using json_encode

24条回答
  •  暖寄归人
    2020-11-22 02:48

    Simple way for php>5.4: like in Facebook graph

    $Data = array('a' => 'apple', 'b' => 'banana', 'c' => 'catnip');
    $json= json_encode($Data, JSON_PRETTY_PRINT);
    header('Content-Type: application/json');
    print_r($json);
    

    Result in browser

    {
        "a": "apple",
        "b": "banana",
        "c": "catnip"
    }
    

提交回复
热议问题