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 numeric index. Here\'s an example code:
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];