Easy peasy lemon squeezy: http://www.php.net/manual/en/function.json-encode.php

<?php
$arr = array('a' => 1, 'b' => 2, 'c' => 3, 'd' => 4, 'e' => 5);

echo json_encode($arr);
?>

There's a post by andyrusterholz at g-m-a-i-l dot c-o-m on the aforementioned page that can also handle complex nested arrays (if that's your thing).

Answer from David Titarenco on Stack Overflow
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-decode.php
PHP: json_decode - Manual
If you want to enforce an array to encode to a JSON list (all array keys will be discarded), use: json_encode(array_values($array)); If you want to enforce an array to encode to a JSON object, use: json_encode((object)$array); See also: ...
Discussions

How can I get JSON objects by array?
Json_decode() returns an object by default but you can provide the correct 2nd parameter to get an indexed array instead. Look it up in the php manual on line (at PHP.net) for specifics. More on reddit.com
๐ŸŒ r/PHPhelp
6
1
March 28, 2024
JsonStream PHP: JSON Streaming Library
You built or Claude built? More on reddit.com
๐ŸŒ r/PHP
27
0
December 11, 2025
Help parsing JSON array using PHP
Test it first with array_key_exists() More on reddit.com
๐ŸŒ r/PHPhelp
4
1
April 20, 2022
Wanting to return JSON from PHP as a two dimensional array/nested array using data from a mySQLi database
I think all you would have to do is create a new associative array where each set of results has its own key. So after the while loop, $result_array would be pushed into a the new array with the table name as the key. $new_array[โ€˜Menuโ€™] = $result_array; Do that for each table then echo json_encode($new_array); Hope that helps. Sorry on mobile. More on reddit.com
๐ŸŒ r/PHPhelp
8
1
March 12, 2021
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 0uwqq41y โ€บ json-to-array-php-example
How do I convert JSON to PHP array?
The json_decode() function accepts ... the data nesting depth exceeds the recursion limit, the converting function will return NULL. In this JSON Convert to PHP Array example, we use the json_decode() function to convert JSON string to a PHP array....
๐ŸŒ
W3Schools
w3schools.com โ€บ php โ€บ php_json.asp
PHP and JSON
PHP has some built-in functions to handle JSON. First, we will look at the following two functions: ... The json_encode() function is used to encode a value to JSON format. This example shows how to encode an associative array into a JSON object:
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-create-an-array-for-json-using-php
How to create an array for JSON using PHP? - GeeksforGeeks
July 11, 2025 - You can also create an array of array of objects with this function. As in JSON, everything is stored as a key-value pair we will convert these key-value pairs of PHP arrays to JSON which can be used to send the response from the REST API server. Example 1: The below example is to convert an ...
๐ŸŒ
Elastic
elastic.co โ€บ elastic docs โ€บ reference โ€บ elasticsearch โ€บ clients โ€บ php โ€บ configuration โ€บ dealing with json arrays and objects in php
Dealing with JSON arrays and objects in PHP | PHP
It makes these nested arrays much simpler to read: $params['body'] = [ 'query' => [ 'match' => [ 'content' => 'quick brown fox' ] ], 'sort' => [ ['time' => ['order' => 'desc']], ['popularity' => ['order' => 'desc']] ] ]; $results = $client->search($params); ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 0kvtdskv โ€บ php-array-to-json-example
How do I convert PHP Array to JSON?
You can customize the conversion ... and JSON output behavior. In this PHP Array to JSON example, we use the json_encode() function to convert a PHP array ......
Find elsewhere
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-encode-array-in-json-php
How to Encode Array in JSON PHP ? - GeeksforGeeks
July 23, 2025 - PHP provides a built-in function json_encode() to encode PHP data structures, including arrays, into JSON format. It automatically handles most data types, making it simple and efficient. Example: The example below shows How to encode an array in JSON PHP Using json_encode().
๐ŸŒ
Quora
quora.com โ€บ How-do-I-make-a-JSON-array-in-PHP
How to make a JSON array in PHP - Quora
There are some examples of php code accessing elements of JSON with foreach. You could change from foreach to for with limits to achieve forsome. ... Hello Friend. To convert PHP array to JSON you simply have to use the json_encode() function.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ handling-json-files-with-php โ€บ lessons โ€บ parsing-json-arrays-with-php
Parsing JSON Arrays with PHP
Here's how you can parse this JSON array using a loop in PHP: In this example, we extract the departments JSON array from the parsed data and iterate over each department using a foreach loop.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ json-to-array-php
Convert JSON to Array in PHP with Online Demo - PHPpot
This example has a JSON string that maps the animal with its count. The output of converting this JSON will return an associative array. See this online demo to get the converted array result from a JSON input. View demo ยท It uses PHP json_decode() with boolean true as its second parameter.
๐ŸŒ
Jsontophp
jsontophp.com
Convert JSON Object to PHP Array Online
JSON to PHP array converter online - Convert the given JSON object or Array into beautified PHP array that can be used instantly into your PHP file as a PHP array
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_php.asp
JSON PHP
Convert the request into an object, using the PHP function json_decode(). Access the database, and fill an array with the requested data.
๐ŸŒ
IBM
ibm.com โ€บ docs โ€บ en โ€บ integration-bus โ€บ 9.0.0
Using PHP arrays with JSON
July 21, 2017 - We cannot provide a description for this page right now
๐ŸŒ
Scout APM
scoutapm.com โ€บ blog โ€บ php-json-encode-serialize-php-objects-to-json
PHP Json_encode: Serialize PHP Objects to JSON
Letโ€™s understand how we can ... json_encode(). <?php class Book { } $book = new Book(); $book->id = 101; $book->label = "Lorem ipsum"; $jsonData = json_encode($book); echo $jsonData."\n"; ?> ... There are three types of arrays ...
๐ŸŒ
Medium
medium.com โ€บ @jochelle.mendonca โ€บ demystifying-json-and-arrays-in-php-unraveling-the-complexity-c8a1c9d51896
Demystifying JSON and Arrays in PHP: Unraveling the Complexity | Medium
March 6, 2024 - The ability to decode JSON data into PHP arrays and encode PHP arrays into JSON format allows developers to easily interface their applications with other systems and APIs, facilitating effective data interchange and interoperability.
๐ŸŒ
ReqBin
reqbin.com โ€บ json โ€บ php โ€บ uzykkick โ€บ json-array-example
PHP | What is JSON Array?
June 11, 2022 - Unlike dictionaries, where you can get the value by its key, in a JSON array, the array elements can only be accessed by their index. The following is an example of a JSON array with numbers. Below, you can find a list of JSON arrays with different data types. The PHP code was automatically ...
๐ŸŒ
W3Resource
w3resource.com โ€บ php-exercises โ€บ file โ€บ php-file-handling-exercise-17.php
PHP JSON file: Read and convert to an associative array
We then use json_decode($jsonContents, true) to decode the JSON string into a PHP associative array. The second argument true specifies that we want an associative array rather than an object. If there is an error decoding the JSON data (e.g., the JSON format is invalid), we throw an exception with the message "Error decoding the JSON data." If the file is successfully read and the JSON data is decoded, we return the resulting associative array from the function. After the function definition, we have an example usage section where we call the readJSONFile($filename) function.
๐ŸŒ
Jonathan Suh
jonsuh.com โ€บ blog โ€บ convert-loop-through-json-php-javascript-arrays-objects
Convert and Loop through JSON with PHP and JavaScript Arrays/Objects โ€” Jonathan Suh
September 30, 2014 - "'"); // Create empty array to ... $row['gender'] ]); } // Convert the Array to a JSON String and echo it $someJSON = json_encode($someArray); echo $someJSON; ?> To get a more in-depth and better example of PHP-JSON-JavaScript/j...