How to beautifully format JSON in PHP? Pretty-Print output
Hello everyone! I'm a beginner in PHP and working on a project where I need to output JSON data in a readable format. The problem is that the standard json_encode() outputs everything in one line, and it's completely unreadable, especially when the data contains nested arrays and objects.\n\nHere's an example of my code:\n\n```php\n 'Ivan Ivanov',\n 'age' => 30,\n 'address' => array(\n 'city' => 'Moscow',\n 'street' => 'Tverskaya',\n 'zip' => '101000'\n ),\n 'phones' => array(\n 'home' => '+7-495-123-45-67',\n 'mobile' => '+7-916-987-65-43'\n ),\n 'hobbies' => array('reading', 'programming', 'chess')\n);\n\n$json = json_encode($data);\necho $json;\n?>\n```\n\nAs a result, I get something like:\n```json\n{"name":"Ivan Ivanov","age"
No answers yet.
Log in to leave an answer
Blogs