How can JSON data with line breaks be written to a file in PHP?
When writing JSON data with line breaks to a file in PHP, you can use the `JSON_PRETTY_PRINT` option in the `json_encode()` function to format the JSON output with line breaks and indentation. This will make the JSON data more readable when written to a file.
$data = ['key1' => 'value1', 'key2' => 'value2'];
$jsonData = json_encode($data, JSON_PRETTY_PRINT);
file_put_contents('output.json', $jsonData);
Keywords
Related Questions
- Can you provide examples or scenarios where unsetting array elements in PHP may lead to unexpected results or errors?
- Are there best practices for setting character encoding, such as using "SET NAMES 'utf8'", to avoid login issues in PHP applications?
- How can the PHP max_execution_time and memory_limit settings impact the performance of a script fetching emails from a POP3 account and inserting them into a database?