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 = "192.168.0.1";
$port = "2016";
array_push($json, ["ip" => $ip, "port" => $port]);
$json = json_encode($json, JSON_PRETTY_PRINT);
// ----- json_decode($json)["ip"] should be "192.168.0.1" ----
echo $json;
This is what I get
[
[
"ip" => "192.168.0.1",
"port" => "2016"
]
]
But I want to get an object instead of array:
{
"0": {
"ip": "192.168.0.1",
"port": "2016"
}
}
Array::forEachandArray::mapfor example).