PHP生成的JSON格式化求助:非表格形式实现目标样式
Hey there! Let me help you figure out how to format your PHP-generated JSON into that clean, structured non-table layout you need. Since you mentioned most solutions you found are table-based, here are straightforward ways to get that nicely indented, human-readable format without tables:
1. Generate Formatted JSON Directly in PHP
When you encode your data with json_encode(), just add the JSON_PRETTY_PRINT flag. This tells PHP to output the JSON with automatic indentation and line breaks—no tables involved.
Example code:
// Your original data array/object $userData = [ "name" => "Alice Smith", "age" => 28, "is_active" => true, "contact" => [ "email" => "alice@example.com", "phone" => "+123456789" ] ]; // Encode with pretty print flag $formattedJson = json_encode($userData, JSON_PRETTY_PRINT); // Output it (wrap in <pre> for proper display in browsers) echo '<pre>' . $formattedJson . '</pre>';
This will output something like:
{ "name": "Alice Smith", "age": 28, "is_active": true, "contact": { "email": "alice@example.com", "phone": "+123456789" } }
2. Format Existing Compressed JSON Strings
If you already have a minified/compressed JSON string (not generated from scratch), you can decode it first, then re-encode with the pretty print flag:
$compressedJson = '{"name":"Alice Smith","age":28,"is_active":true,"contact":{"email":"alice@example.com","phone":"+123456789"}}'; // Decode to PHP array/object $decodedData = json_decode($compressedJson, true); // Re-encode with formatting $formattedJson = json_encode($decodedData, JSON_PRETTY_PRINT); echo '<pre>' . $formattedJson . '</pre>';
3. Custom Formatting (If You Need More Control)
If the standard pretty-printed JSON isn't exactly matching your target style, you can build a custom function to traverse your data and output it with your preferred indentation, spacing, or syntax:
function customFormatJson($data, $indentLevel = 0) { $indent = str_repeat(' ', $indentLevel); // Use 2 spaces instead of 4, adjust as needed $output = ''; if (is_array($data)) { $output .= "{\n"; $firstItem = true; foreach ($data as $key => $value) { if (!$firstItem) $output .= ",\n"; $firstItem = false; $output .= $indent . ' "' . $key . '": '; if (is_array($value)) { $output .= customFormatJson($value, $indentLevel + 1); } elseif (is_string($value)) { $output .= '"' . $value . '"'; } else { $output .= var_export($value, true); // Handles booleans/numbers } } $output .= "\n" . $indent . "}"; } return $output; } // Usage echo '<pre>' . customFormatJson($userData) . '</pre>';
This lets you tweak things like indent size, line breaks, or even add custom comments if needed.
Key Note for Web Display
Always wrap your formatted JSON in a <pre> tag when displaying it in a browser—otherwise, HTML will collapse all the indentation and line breaks, making it look messy.
That should cover your needs! The JSON_PRETTY_PRINT method is the simplest and most reliable for most cases, while the custom function gives you full control if you need a specific style.
内容的提问来源于stack exchange,提问作者prodisoft

