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
🌐
PHP
php.net β€Ί manual β€Ί en β€Ί function.json-encode.php
PHP: json_encode - Manual
Returns a string containing the JSON representation of the supplied value. If the parameter is an array or object, it will be serialized recursively. If a value to be serialized is an object, then by default only publicly visible properties will be included.
🌐
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:
Discussions

PHP Array to Json Object - Stack Overflow
I need to convert a PHP array to JSON but I don't get what I expect. I want it to be an object that I can navigate easily with a numeric index. Here's an example code: $json = array(); $ip = "... More on stackoverflow.com
🌐 stackoverflow.com
php - How to convert JSON string to array - Stack Overflow
What I want to do is the following: taking JSON as input from text area in php use this input and convert it to JSON and pass it to php curl to send request. this m getting at php from get of api ... More on stackoverflow.com
🌐 stackoverflow.com
[HELP] json_decode on PHP fails to convert an array of JSON objects
can you C/P error. I don't know why are you using json_decode. Can you try without json_decode?. Sample::insert($samples); More on reddit.com
🌐 r/laravel
9
0
December 14, 2016
How to convert JSON string to PHP array?
$array = json_decode($json, true);
More on reddit.com
🌐 r/PHPhelp
19
0
June 8, 2017
🌐
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.
🌐
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
🌐
Scout APM
scoutapm.com β€Ί blog β€Ί php-json-encode-serialize-php-objects-to-json
PHP Json_encode: Serialize PHP Objects to JSON
This will result in an Associative array being returned. ... In this post, we read about the JSON (JavaScript Object Notation) format, why it is essential, and how we can convert different PHP variables into JSON using json_encode(). We also saw how we could use json_decode() to decode JSON data into PHP variables.
Find elsewhere
🌐
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.
🌐
CodeSignal
codesignal.com β€Ί learn β€Ί courses β€Ί handling-json-files-with-php β€Ί lessons β€Ί parsing-json-arrays-with-php
Parsing JSON Arrays with PHP
Welcome to this lesson on parsing arrays within JSON structures using PHP. As you've learned in previous lessons, JSON (JavaScript Object Notation) is a popular data format used for exchanging data due to its simplicity and readability. JSON arrays are crucial for representing collections of ...
🌐
W3Schools
w3schools.com β€Ί php β€Ί func_json_encode.asp
PHP json_encode() Function
Arrays Indexed Arrays Associative ... Arrays Array Functions PHP Superglobals Β· Superglobals $GLOBALS $_SERVER $_REQUEST $_POST $_GET PHP RegEx PHP RegEx Functions Β· PHP Form Handling PHP Form Validation PHP Form Required PHP Form URL/E-mail PHP Form Complete Β· PHP Date and Time PHP Include PHP File Handling PHP File Open/Read PHP File Create/Write PHP File Upload PHP Cookies PHP Sessions PHP Filters PHP Filters Advanced PHP Callback Functions PHP JSON PHP ...
🌐
Juha-Matti Santala
hamatti.org β€Ί snacks β€Ί force-empty-php-array-to-become-json-object
Force empty PHP array to become JSON object : Juha-Matti Santala
There are no empty associative arrays in PHP so to get one converting correctly in JSON, you need extra casting.
🌐
ReqBin
reqbin.com β€Ί code β€Ί php β€Ί 0uwqq41y β€Ί json-to-array-php-example
How do I convert JSON to PHP array?
PHP has two types of arrays: indexed arrays and associative arrays. 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.
🌐
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.
🌐
Wtools
wtools.io β€Ί convert-php-array-to-json
Convert PHP Array to JSON Online | WTOOLS
Free tool for online converting PHP array into JSON object, generate JSON from PHP array.
🌐
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 - PHP >= 5.2.0 features a function, json_decode, that decodes a JSON string into a PHP variable. By default it returns an object. The second parameter accepts a boolean that when set as true, tells it to return the objects as associative arrays.
🌐
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.
🌐
Netlify
json2php.netlify.app
JSON to PHP - json_decode online
We're sorry but JSON to PHP doesn't work properly without JavaScript enabled. Please enable it to continue
🌐
ExtendsClass
extendsclass.com β€Ί json-to-php.html
JSON to PHP array
PHP is a language which allows quite easily to manipulate data. Since JSON format is used a lot, chances are some people need to convert JSON data in order to use it in their PHP code. In PHP language, it is possible to use the json_decode function to convert JSON code to a PHP object / array.