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 Top answer 1 of 16
1542
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);
2 of 16
205
This function will take JSON string and indent it very readable. It also should be convergent,
prettyPrint( $json ) === prettyPrint( prettyPrint( $json ) )
Input
{"key1":[1,2,3],"key2":"value"}
Output
{
"key1": [
1,
2,
3
],
"key2": "value"
}
Code
function prettyPrint( $json )
{
$result = '';
$level = 0;
$in_quotes = false;
$in_escape = false;
$ends_line_level = NULL;
$json_length = strlen( $json );
for(
i < $json_length; $i++ ) {
$char = $json[
new_line_level = NULL;
$post = "";
if( $ends_line_level !== NULL ) {
$new_line_level = $ends_line_level;
$ends_line_level = NULL;
}
if ( $in_escape ) {
$in_escape = false;
} else if( $char === '"' ) {
$in_quotes = !$in_quotes;
} else if( ! $in_quotes ) {
switch( $char ) {
case '}': case ']':
$level--;
$ends_line_level = NULL;
$new_line_level = $level;
break;
case '{': case '[':
$level++;
case ',':
$ends_line_level = $level;
break;
case ':':
$post = " ";
break;
case " ": case "\t": case "\n": case "\r":
$char = "";
$ends_line_level = $new_line_level;
$new_line_level = NULL;
break;
}
} else if ( $char === '\\' ) {
$in_escape = true;
}
if( $new_line_level !== NULL ) {
$result .= "\n".str_repeat( "\t", $new_line_level );
}
$result .= $char.$post;
}
return $result;
}
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.
Videos
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.
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.
CodeSignal
codesignal.com โบ learn โบ courses โบ handling-json-files-with-php โบ lessons โบ working-with-json-data-using-phps-class-structures-and-serialization
Constructing and Writing JSON Data with PHP
With our data structure populated, we can serialize it into JSON format using PHP's json_encode function.
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
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.