且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PHP数组到Json对象

更新时间:2023-01-17 14:55:17

您要json_encode($json, JSON_FORCE_OBJECT).

顾名思义, JSON_FORCE_OBJECT 标志强制json输出为对象,即使通常将其表示为数组也是如此.

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.

您也可以删除array_push来使用一些更简洁的代码:

You can also eliminate the use of array_push for some slightly cleaner code:

$json[] = ['ip' => $ip, 'port' => $port];