PHP 5.4 offers the JSON_PRETTY_PRINT option for use with the json_encode() call.

https://php.net/manual/en/function.json-encode.php

<?php
...
$json_string = json_encode($data, JSON_PRETTY_PRINT);
Answer from candieduniverse on Stack Overflow
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_json.asp
PHP and JSON
PHP has some built-in functions to handle JSON. First, we will look at the following two functions: ... The json_encode() function is used to encode a value to JSON format.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-encode.php
PHP: json_encode - Manual
For example, the string "Example\" will cause the following error in JavaScript when parsing: JSON.parse: expected ',' or '}' after property value in object This appears to be an oversight in the JSON specification and the only work-around appears to be removing trailing slashes from all strings before encoding. ... Be careful with floating values in some locales (e.g. russian) with comma (",") as decimal point. Code: <?php setlocale(LC_ALL, 'ru_RU.utf8'); $arr = array('element' => 12.34); echo json_encode( $arr ); ?> Output will be: -------------- {"element":12,34} -------------- Which is NOT a valid JSON markup.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ json-handling-with-php-how-to-encode-write-parse-decode-and-convert
JSON Handling with PHP: How to Encode, Write, Parse, Decode and Convert - PHPpot
The PHP json_decode function will default convert the JSON data into an object. The $assoc parameter of the json_decode function will force the output format based on the boolean value passed to it.
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ html โ€บ php-json-pretty-print
PHP JSON Pretty Print - GeeksforGeeks
August 5, 2025 - JSON is a JavaScript notation for storing and fetching data. Data is usually stored in JSON, XML, or in some other database. It is a complete language-independent text format. To work with JSON data, PHP uses JSON_PRETTY_PRINT.
๐ŸŒ
Nidup
nidup.io โ€บ blog โ€บ manipulate-json-files-in-php
Read, Decode, Encode, Write JSON in PHP | Nicolas Dupont
March 12, 2022 - The second parameter of json_encode specifies the format of the string. We can go for the compact option by default.
๐ŸŒ
ZetCode
zetcode.com โ€บ php โ€บ json
PHP JSON - working with JSON in PHP
PHP JSON tutorial shows how to work with JSON in PHP. JSON is a lightweight data-interchange format.
๐ŸŒ
Code Beautify
codebeautify.org โ€บ blog โ€บ json-encode-pretty-print-using-php
Json_Encode() Pretty Print Using PHP
January 16, 2023 - Json_Encode() is a function in PHP that takes a data structure (array, php objects, associative array) as input and returns a JSON encoded string. Itโ€™s inbuilt function of PHP to convert PHP array, objects and generate JSON string.
Find elsewhere
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to pretty-print json in php?
How To Pretty-Print JSON In PHP?
October 15, 2024 - PHP 5.4 and later versions have a built-in solution for pretty-printing JSON: the JSON_PRETTY_PRINT option. You can use this option with the json_encode() function to create formatted, readable JSON output.
๐ŸŒ
Scout APM
scoutapm.com โ€บ blog โ€บ php-json-encode-serialize-php-objects-to-json
PHP Json_encode: Serialize PHP Objects to JSON
The function takes in a PHP object ($value) and returns a JSON string (or False if the operation fails). Donโ€™t worry about the $options and $depth parameters; youโ€™re seldom going to need them. ... As we saw above, JSON is one of the most common formats for exchanging data over the internet.
๐ŸŒ
Tutorial Republic
tutorialrepublic.com โ€บ php-tutorial โ€บ php-json-parsing.php
How to Encode and Decode JSON Data in PHP - Tutorial Republic
JSON is the most popular and lightweight data-interchange format for web applications. JSON data structures are very similar to PHP arrays. PHP has built-in functions to encode and decode JSON data.
๐ŸŒ
Brj
en.php.brj.cz โ€บ json-in-php---processing-generation-and-formatting
Json in PHP - processing, generation and formatting
The Json data format was created to facilitate the transfer of structured data between client and server. To convert an array or object to Json, there is a function json_encode in PHP:
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_php.asp
JSON PHP
Convert the object into a JSON string. Send a request to the PHP file, with the JSON string as a parameter.
๐ŸŒ
Scaler
scaler.com โ€บ home โ€บ topics โ€บ php-tutorial โ€บ how to create json objects in php
How to Create JSON Objects in PHP - Scaler Topics
April 14, 2024 - Creating JSON in PHP involves encoding PHP data structures into JSON format for data interchange. The json_encode() function is pivotal, converting arrays and objects into their JSON equivalents.
๐ŸŒ
GitHub
github.com โ€บ edmundgentle โ€บ json-format
GitHub - edmundgentle/json-format: A PHP function to format a JSON string in a human-readable manner.
require '../src/format_json.php'; // Create a random array, and fill it with some sample data $sample_array = array( "foo" => "bar", "bar" => "foo", "hello" => array( "jim", "world" ), "multi" => array( "dimensions"=>true ) ); // Encode the array into a JSON string $json_string = json_encode( $sample_array ); // Output the formatted string echo format_json( $json_string );
Author ย  edmundgentle
๐ŸŒ
Uptimia
uptimia.com โ€บ home โ€บ questions โ€บ how to return json from a php script?
How To Return JSON From A PHP Script?
October 5, 2024 - To convert PHP data structures into JSON format, use the json_encode() function.
๐ŸŒ
Linux Hint
linuxhint.com โ€บ pretty_json_php
Linux Hint โ€“ Linux Hint
Linux Hint LLC, [email protected] 1210 Kelly Park Circle, Morgan Hill, CA 95037 Privacy Policy and Terms of Use
๐ŸŒ
PHP Tutorial
phptutorial.net โ€บ home โ€บ php oop โ€บ php json
An Essential Guide to PHP JSON
July 21, 2021 - PHP natively supports JSON via the JSON extension. The JSON extension provides you with some handy functions that convert data from PHP to JSON format and vice versa.