0
0
PhpHow-ToBeginner · 2 min read

PHP How to Convert Array to JSON String

Use the json_encode() function in PHP to convert an array to a JSON string, like json_encode($array).
📋

Examples

Input['apple', 'banana', 'cherry']
Output["apple","banana","cherry"]
Input["name" => "John", "age" => 30]
Output{"name":"John","age":30}
Input[]
Output[]
🧠

How to Think About It

To convert an array to JSON in PHP, think of turning a list or dictionary into a text format that other programs can read. PHP has a built-in function called json_encode() that takes your array and changes it into a JSON string automatically.
📐

Algorithm

1
Get the array you want to convert.
2
Call the <code>json_encode()</code> function with the array as input.
3
Store or output the returned JSON string.
💻

Code

php
<?php
$array = ['name' => 'Alice', 'age' => 25, 'city' => 'Paris'];
$json = json_encode($array);
echo $json;
?>
Output
{"name":"Alice","age":25,"city":"Paris"}
🔍

Dry Run

Let's trace converting ['name' => 'Alice', 'age' => 25, 'city' => 'Paris'] to JSON.

1

Input array

array = ['name' => 'Alice', 'age' => 25, 'city' => 'Paris']

2

Call json_encode

json_encode(array) returns '{"name":"Alice","age":25,"city":"Paris"}'

3

Output JSON string

Output: '{"name":"Alice","age":25,"city":"Paris"}'

StepActionValue
1Input array{"name":"Alice","age":25,"city":"Paris"}
2Call json_encode{"name":"Alice","age":25,"city":"Paris"}
3Output JSON string{"name":"Alice","age":25,"city":"Paris"}
💡

Why This Works

Step 1: Built-in function

PHP provides json_encode() to convert arrays or objects into JSON strings easily.

Step 2: Input array

You pass your PHP array to json_encode() as the input parameter.

Step 3: Output JSON

The function returns a string formatted as JSON, which can be used for data exchange or storage.

🔄

Alternative Approaches

Manual string building
php
<?php
$array = ['a', 'b'];
$json = '[' . implode(',', array_map(fn($v) => '"' . $v . '"', $array)) . ']';
echo $json;
?>
This method is error-prone and does not handle nested arrays or special characters well, so it's not recommended.
Using json_encode with options
php
<?php
$array = ['name' => 'Bob', 'age' => 28];
$json = json_encode($array, JSON_PRETTY_PRINT);
echo $json;
?>
Adds formatting to JSON output for readability but increases output size.

Complexity: O(n) time, O(n) space

Time Complexity

The function processes each element of the array once, so time grows linearly with array size.

Space Complexity

The output JSON string size depends on the input array size, requiring additional memory proportional to input.

Which Approach is Fastest?

Using PHP's built-in json_encode() is fastest and safest compared to manual string building.

ApproachTimeSpaceBest For
json_encode()O(n)O(n)All array to JSON conversions
Manual string buildingO(n)O(n)Simple flat arrays only, not recommended
json_encode() with optionsO(n)O(n)Readable JSON output
💡
Always use json_encode() to safely convert arrays to JSON in PHP.
⚠️
Trying to build JSON strings manually instead of using json_encode() leads to errors and invalid JSON.