I am trying to create a JSON array through PHP in that format:
{
"Commands":[
{
"StopCollection":true
},
{
"Send":false
},
{
"HeartbeatSend":60
}
]
}
The closest I got to do that is:
by using JSON_FORCE_OBJECT
$commands = array();
$commands['Commands'] = array();
array_push($commands['Commands'],array('StopCollection' => true));
array_push($commands['Commands'],array('Send' => false));
array_push($commands['Commands'],array('HeartbeatSend' => 60));
$jsonCommands = json_encode($commands, JSON_FORCE_OBJECT);
Which outputs
{
"Commands":{
"0":{
"StopCollection":true
},
"1":{
"Send":false
},
"2":{
"HeartbeatSend":60
}
}
}
And using (object)
$commands = (object) [
'Commands' => [
'StopCollection' => true,
'Send' => false,
'HeartbeatSend' => 60
]
];
$jsonCommands = json_encode($commands);
Which outputs
{
"Commands":{
"StopCollection":true,
"Send":false,
"HeartbeatSend":60
}
}
Both are close but I need Commands to be an array of objects without a key. How do I do that?