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 OverflowPHP Array to Json Object - Stack Overflow
Convert Array to JSON string php - Stack Overflow
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.)
toArray() method versus iterator_to_array function
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];
just use only
$response=array();
$response["0"]=array("ip" => "192.168.0.1",
"port" => "2016");
$json=json_encode($response,JSON_FORCE_OBJECT);