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:
$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"
}
}
Thank you :)
Array::forEach
andArray::map
for example). – 00Davo yesterday