Sai A Sai A
Updated date Nov 20, 2023
In this blog, we will learn different methods to convert an array into a formatted JSON string in PHP, making your data more readable and presentable.

Introduction:

In web development, managing data is an important task. Whether you are working with databases, APIs, or even simple data structures within your PHP applications, you often need to convert this data into a structured and readable format. One common format for data interchange is JSON (JavaScript Object Notation), which is both human-readable and easy for machines to parse. But what if you have an array in PHP that you want to convert to a JSON string, and you want it to be nicely formatted for easier reading and debugging?

In this blog, we will explore different methods to convert a PHP array into a pretty printed JSON string. Pretty printing, or formatting, adds indentation and line breaks to the JSON string, making it more human-friendly. We will cover multiple approaches to achieve this conversion.

Method 1: Using json_encode() with the JSON_PRETTY_PRINT Option

The simple way to convert an array into a pretty printed JSON string is by using the json_encode() function with the JSON_PRETTY_PRINT option. This option was introduced in PHP 5.4 and makes the JSON output nicely formatted.

<?php
$arrayData = [
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York',
];

$jsonString = json_encode($arrayData, JSON_PRETTY_PRINT);

echo $jsonString;
?>

Output:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

In this example, the json_encode() function is used to convert the $arrayData array into a JSON string, and the JSON_PRETTY_PRINT option ensures the output is nicely formatted with proper indentation.

Method 2: Using json_encode() and Custom Formatting

While the JSON_PRETTY_PRINT option is the simplest way to pretty print JSON, you can also achieve custom formatting by formatting the JSON string manually. This approach gives you more control over how the JSON is formatted.

<?php
$arrayData = [
    'name' => 'John Doe',
    'age' => 30,
    'city' => 'New York',
];

$jsonString = json_encode($arrayData, JSON_UNESCAPED_UNICODE);

$prettyJson = '';
$indentation = 0;
$len = strlen($jsonString);

for ($i = 0; $i < $len; $i++) {
    $char = $jsonString[$i];
    if ($char === '{' || $char === '[') {
        $prettyJson .= $char . "\n" . str_repeat('    ', ++$indentation);
    } elseif ($char === '}' || $char === ']') {
        $prettyJson .= "\n" . str_repeat('    ', --$indentation) . $char;
    } elseif ($char === ',') {
        $prettyJson .= $char . "\n" . str_repeat('    ', $indentation);
    } else {
        $prettyJson .= $char;
    }
}

echo $prettyJson;
?>

Output:

{
    "name": "John Doe",
    "age": 30,
    "city": "New York"
}

In this example, we manually format the JSON string by adding newlines and indentation for better readability. This method provides more control over the formatting but may be a bit more complex to implement for large JSON structures.

Conclusion:

In this blog, we have explored different methods for converting a PHP array into a pretty printed JSON string. We began with the simplest and most common approach using the json_encode() function with the JSON_PRETTY_PRINT option, which automatically adds formatting to the JSON output. This method is suitable for most scenarios where you need readable JSON data. We also discussed a custom formatting approach, which allows for more control over the formatting of the JSON string. This method can be useful if you have specific requirements for the structure and presentation of your JSON data.

Comments (0)

There are no comments. Be the first to comment!!!