You want to json_encode($json, JSON_FORCE_OBJECT).

The JSON_FORCE_OBJECT flag, as the name implies, forces the json output to be an object, even when it otherwise would normally be represented as an array.

You can also eliminate the use of array_push for some slightly cleaner code:

$json[] = ['ip' => $ip, 'port' => $port];
Answer from jbafford on Stack Overflow
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ php โ€บ how-to-convert-php-array-to-javascript-or-json
How to convert PHP array to JavaScript or JSON ? - GeeksforGeeks
July 12, 2025 - json_encode( $my_array ); Example 1: This example uses json_encode() function to convert PHP array to JavaScript JSON object.
Discussions

Convert php array to Javascript - Stack Overflow
PHP provides a function to convert PHP arrays into Javascript code: json_encode(). (technically, it's JSON format; JSON stands for JavaScript Object Notation) More on stackoverflow.com
๐ŸŒ stackoverflow.com
jquery - convert php array into single JSON object - Stack Overflow
Bring the best of human thought and AI automation together at your work. Explore Stack Internal ... Save this question. Show activity on this post. I converted a PHP array into JSON, using json_encode. I checked the console, and the objects are displaying in array, but as individual objects. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Javascript Convert PHP Json into a javascript array - Stack Overflow
As an explanation it might be helpful to note that JSON (JavaScript Object Notation) is a subset of the JavaScript language and so can be parsed successfully by any JavaScript interpreter. 2012-12-21T17:58:45.057Z+00:00 ... Indeed, JSON!=JavaScript, it's just a serialized representation of data that JavaScript (and many other languages) can translate. 2012-12-21T18:00:20.287Z+00:00 ... I reccomend using jquery. The php file should look as such ... //location.php More on stackoverflow.com
๐ŸŒ stackoverflow.com
Javascript Array Is Getting Converted To A String in PHP File?
"The field's value is the second argument to append. This can be a string or Blob (including subclasses such as File). If none of these are specified the value is converted to a string." https://developer.mozilla.org/en-US/docs/Web/API/FormData/append More on reddit.com
๐ŸŒ r/PHPhelp
12
8
May 31, 2023
๐ŸŒ
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 - If youโ€™re working with JSON (JavaScript Object Notation) and either need to convert a JSON string to array or object and loop through it or vice-versa, take an array or object and convert it to a JSON string to return, both can be done in PHP or JavaScript.
๐ŸŒ
PHP
php.net โ€บ manual โ€บ en โ€บ function.json-encode.php
PHP: json_encode - Manual
When encoded to JSON, this can lead to the list being interpreted as a JSON object instead of a JSON array, which might not be what your JavaScript code expects. The Solution: --------------- The array_values() function is essential in this scenario.
๐ŸŒ
Medium
inyomanjyotisa.medium.com โ€บ how-to-convert-a-php-array-to-json-c0bf63df5b33
How to Convert a PHP Array to JSON | by I Nyoman Jyotisa | Medium
October 18, 2024 - How to Convert a PHP Array to JSON Converting a PHP array to JSON is straightforward using the json_encode() function. Here's an example: $array = [ "foo" => "bar", "baz" => "qux", ]; $json โ€ฆ
Find elsewhere
๐ŸŒ
Medium
sergheipogor.medium.com โ€บ how-to-make-php-arrays-into-javascript-arrays-5d9108f4ea85
How to Make PHP Arrays into JavaScript Arrays | by Serghei Pogor | Medium
March 30, 2024 - Another simple method involves directly assigning the PHP array to a JavaScript variable using json_encode.
๐ŸŒ
sebhastian
sebhastian.com โ€บ php-array-to-javascript-array
Convert PHP array to JavaScript array (with code examples) | sebhastian
September 19, 2022 - When you have an associative PHP array, calling json_encode() will transform the array into a JavaScript object.
๐ŸŒ
Scout APM
scoutapm.com โ€บ home โ€บ blog โ€บ php json_encode: serialize php objects to json
PHP Json_encode: Serialize PHP Objects to JSON | Scout Monitoring
May 1, 2020 - Letโ€™s see how we can convert our PHP variables into JSON format. json_encode() is a native PHP function that allows you to convert PHP data into the JSON format. json_encode ( mixed $value [, int $options = 0 [, int $depth = 512 ]] ) : string ...
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 0kvtdskv โ€บ php-array-to-json-example
How do I convert PHP Array to JSON?
To convert a PHP array to JSON data string, you can use the json_encode($value, $flags, $depth) function. The json_encode() function takes a PHP array as input and returns a JSON string representation.
๐ŸŒ
DotIAM Tools
tools.dotiam.com โ€บ home โ€บ php array to json
PHP Array to JSON Converter - Convert PHP to JSON Online | Free Tool | DotIAM Tools
PHP-specific features that don't map to JSON: PHP resources (file handles, database connections), objects with methods (JSON only stores properties), undefined values, integer-keyed associative arrays (JSON object keys must be strings), and circular references. Our converter handles simple data arrays โ€” complex object graphs may need manual review. ... Common use cases: creating API responses from PHP backend, saving data to localStorage from Ajax calls, passing data from PHP to JavaScript on the same page (use json_encode in PHP variable), storing structured data in database JSON columns, and creating configuration files in JSON format from PHP-based admin panels.
๐ŸŒ
W3Schools
w3schools.com โ€บ js โ€บ js_json_server.asp
<h1>JavaScript Loading JSON</h1>
The request body must contain text, not a JavaScript object. Use JSON.stringify() to convert the object into JSON text.
๐ŸŒ
ProxyScrape
proxyscrape.com โ€บ tools โ€บ php-to-json
PHP Array to JSON Converter - ProxyScrape Tools
Free online tool to convert PHP arrays to JSON format. Convert PHP array syntax to valid JSON objects and arrays with proper formatting. Perfect for PHP developers working with APIs.
๐ŸŒ
AppDevTools
appdevtools.com โ€บ php-array-to-json-converter
PHP Array to JSON Converter - App DevTools
PHP Array to JSON is a tool that converts a PHP array to JSON data. Enter a PHP array in the input field and click the convert button.
๐ŸŒ
ReqBin
reqbin.com โ€บ code โ€บ php โ€บ 0uwqq41y โ€บ json-to-array-php-example
How do I convert JSON to PHP array?
PHP provides many built-in functions for manipulating arrays, including sorting, searching, checking if an element exists in an array, splitting string to array, converting array to string, converting array to JSON, and getting the length of an array. JSON (JavaScript Object Notation) is a language-independent text format for storing and exchanging data.
๐ŸŒ
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.
๐ŸŒ
bishrulhaq
bishrulhaq.com โ€บ home โ€บ posts โ€บ convert php array to json : examples
Convert PHP Array To JSON : Examples | BH
July 1, 2026 - <?php $names = ['Bishrul', 'Bishrul ... :".$namesJSON; ... To Convert a key-value pair array (Associative Array), you can also use json_encode() to convert objects, arrays in PHP to JSON format....