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 OverflowYou 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);
How to convert this PHP array of objects to json? - Stack Overflow
[HELP] json_decode on PHP fails to convert an array of JSON objects
Opis JSON Schema: Is it possible to generate a schema from a PHP array or object?
How to loop this JSON object from php?
When would I need to convert PHP array to JSON?
What PHP array features cannot be converted to JSON?
The best way is to implement JsonSerializable class and define the jsonSerialize() function
Example:
class MyObj implements JsonSerializable{
protected $id;
protected $name;
function getId() { return $this->$id; }
function getName() { return $this->$name; }
public function jsonSerialize() {
return [
'Id' => $this->getId(),
'Name' => $this->getName()
];
}
}
// $arrofobj is array of objects of type MyObj
json_encode($arrofobj); //this will convert array of objects to JSON
If these classes cannot implement JsonSerializable you may access the protected properties using Reflection.
Example:
class Publisher {
protected $name;
protected $id;
public function __construct($id, $name) {
$this->id = $id;
$this->name = $name;
}
}
function getPublisherExportData($publishers) {
$out = [];
foreach($publishers as $publisher) {
$currentPData = [];
$refl = new ReflectionClass($publisher);
$props = $refl->getProperties(ReflectionProperty::IS_PROTECTED);
foreach($props as $prop) {
$prop->setAccessible(true);
$currentPData[$prop->getName()] = $prop->getValue($publisher);
}
$out[] = $currentPData;
}
return $out;
}
var_dump(json_encode(getPublisherExportData([new Publisher(12, 'My Name'), new Publisher(15, 'NameProperty')]), JSON_PRETTY_PRINT));
Will result in:
string(121) "[ { "name": "My Name", "id": 12 }, { "name": "NameProperty", "id": 15 } ]"
However, you will need to handle object graphs and conditional exports yourself. Depending on the required json output you might think about replacing $out with an instance of stdClass and write using the $out->{$prop->getName()} = $val syntax to it.
If you are able to use your custom implementations you could add a method using get_object_vars($this) to avoid most of the code above, however if you could do that you should consider implementing JsonSerializable which is meant for that task.