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
๐ŸŒ
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.
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
Convert Array to JSON string php - Stack Overflow
But i don't want { at the begening and end of the string,bcz this json is request of an api service,when i passw this json i wlll get an error from api, 2016-01-22T09:14:56.437Z+00:00 ... Sorry but then don't use that service because it is asking you to produce invalid JSON 2016-01-22T09:1... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Properly convert JSON with arrays to CSV

Any solution is going to be fragile as you're going from a more expressive to a less expressive format. As long as you can guarantee that all your nested objects (milestone, assignee and author) will always have the same order of elements and will never themselves contain an object or array, you should be able to simply implode them:

Instead of:

foreach ($line['author'] as $value)
{
    array_push($line_array,$value);
}

Use:

array_push($line_array, implode('|', $line['author']));

(Side note: it's generally better to do $myarr[] = 'val'; than array_push($myarr, 'val'). More readable and slightly faster.)

More on reddit.com
๐ŸŒ r/PHPhelp
3
2
April 20, 2015
toArray() method versus iterator_to_array function
One benefit to toArray() is that its behavior could be customized to differ from iterator_to_array()'s if the writer of the class thought it was necessary. And the fact that you don't need to mark your class as Traversable or ArrayAccess. But I'm not sure if these are the reasons toArray() is being used. More on reddit.com
๐ŸŒ r/PHP
15
5
June 5, 2014
๐ŸŒ
DotIAM Tools
tools.dotiam.com โ€บ home โ€บ php array to json
PHP Array to JSON Converter - Convert PHP to JSON Online | Free Tool | DotIAM Tools
Configure: Choose pretty print for readable JSON or compact for minified output. Enable sort keys for consistent output. Escape Unicode if needed. Convert: Click "Convert to JSON" to transform PHP array into valid JSON format.
๐ŸŒ
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.
๐ŸŒ
PHPpot
phppot.com โ€บ php โ€บ php-array-to-json
PHP Array to JSON String Convert with Online Demo - PHPpot
Creating an online tool to convert a PHP array to a JSON object and handling pre-post actions of the array to JSON conversion.
Find elsewhere
๐ŸŒ
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 โ€ฆ
๐ŸŒ
Wtools
wtools.io โ€บ convert-php-array-to-json
Convert PHP Array to JSON Online - wtools.io
This form allows you convert PHP array to JSON, you only need to insert the array syntax [...] or array(...), paste or upload your array below:
๐ŸŒ
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.
๐ŸŒ
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.
๐ŸŒ
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 ... a key-value pair array (Associative Array), you can also use json_encode() to convert objects, arrays in PHP to JSON format....
๐ŸŒ
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 - Read more about the json_encode function from PHPโ€™s documentation. <?php // Array $someArray = [ [ "name" => "Jonathan Suh", "gender" => "male" ], [ "name" => "William Philbin", "gender" => "male" ], [ "name" => "Allison McKinnery", "gender" ...
๐ŸŒ
HandyDevTools
shivasaxena.com โ€บ handydevtools โ€บ json & data tools โ€บ php โ†” json
PHP Array to JSON Converter & JSON to PHP Online | HandyDevTools
Convert between PHP arrays/objects and JSON format. Handle PHP serialized data, associative arrays, and nested structures. Free online PHP-JSON converter.
๐ŸŒ
DevOps Consulting
devopsconsulting.in โ€บ blog โ€บ convert-array-to-json-in-php
Convert Array to JSON in PHP - DevOps Consulting
October 1, 2024 - If your array contains special ... "Espaรฑa" ); // Convert to JSON with special characters unescaped $jsonData = json_encode($data, JSON_UNESCAPED_UNICODE); // Display the JSON echo $jsonData;...
๐ŸŒ
Jsontotable
jsontotable.org โ€บ php-to-json
PHP to JSON Converter - Serialize PHP with json_encode | PHP JSON Serializer | JSON to Table Converter
PHP to JSON conversion transforms PHP data structures like arrays, associative arrays, and objects into standard JSON format. This process converts PHP-specific syntax (array(), =>) into JSON-compliant syntax for data interchange and API communication.
๐ŸŒ
Laracasts
laracasts.com โ€บ discuss โ€บ channels โ€บ tips โ€บ convert-php-array-to-json-in-blade-file
Convert php array to json in blade file
The @json($locations) directive is used to convert the PHP array to a JSON object that can be used in JavaScript.