How to create an array for JSON using PHP?

How to create an array for JSON using PHP?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Building JSON Arrays in PHP: From Data Structure to String Output

Creating a JSON array from PHP data is a fundamental task when building APIs or preparing responses for frontend applications. While it might seem as simple as wrapping your data in brackets, the actual process relies on converting native PHP structures into a specific string format that adheres to JSON standards. The most reliable way to achieve this is by leveraging PHP's built-in serialization functions rather than attempting manual string manipulation.

The standard approach involves using the json_encode() function. This function accepts an array or object as input and returns a JSON-encoded string. For instance, if you have a simple indexed array containing associative arrays—like the example below—you can pass that structure directly to the function:

$data = [
    ['region' => 'valore', 'price' => 'valore2'],
    ['region' => 'valore', 'price' => 'valore2'],
    ['region' => 'valore', 'price' => 'valore2']
];

$jsonString = json_encode($data);
echo $jsonString;

This output will exactly match the desired JSON format: [{"region":"valore","price":"valore2"},...]. The function handles the necessary escaping of quotes and ensures proper comma separation between elements. However, there is a critical decision to make regarding the output format: should the result be an indexed array or an associative array? In PHP, this distinction is controlled by the $assoc parameter of json_encode().

If you pass false as the second argument (which is the default behavior), json_encode() will convert PHP objects into JSON objects where keys remain strings. If you pass true, it forces the conversion of object properties to string keys in the resulting JSON, which can be important when your data source relies on property access rather than key-based indexing. This distinction becomes particularly relevant if you are working with classes that represent entities, as changing this flag alters how the object's internal structure is exposed to the consumer of your API.

Another frequent pitfall involves the handling of special characters and encoding. PHP strings must be compatible with UTF-8 for json_encode() to handle non-ASCII characters correctly, especially when dealing with emojis or accented letters. If you encounter issues with invalid UTF-8 sequences causing errors, ensure your source data is properly encoded. Additionally, newer versions of PHP allow you to specify options using the third argument, such as JSON_UNESCAPED_UNICODE, which prevents the function from escaping unicode characters unnecessarily, making the output more readable for developers who know they are handling valid UTF-8 text.

While manual string concatenation or regex-based approaches might seem like quick fixes, they are fragile and prone to introducing syntax errors that break JSON parsers on the client side. Relying on json_encode() ensures validation against strict RFC 8259 standards. It handles edge cases like null values, booleans, and numeric types automatically, converting them to their JSON equivalents (null, true/false, and numbers without quotes) without developer intervention.

For projects built with Laravel, the framework provides a convenient helper method that wraps this functionality. You can access the underlying encoder via \Illuminate\Support\Facades\Json::encode($data). This is particularly useful when integrating JSON generation into larger application logic where you might need to toggle between different encoding options or handle exceptions related to invalid data types.

Ultimately, the goal is to produce a consistent, valid JSON string that clients can parse reliably. By sticking to built-in serialization functions and understanding the nuances of flags like $assoc and encoding options, you avoid subtle bugs that often plague manual implementation attempts.

Our network