PHP 数组到 Json 对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/35099682/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP Array to Json Object
提问by Hassan Ila
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:
我需要将一个 PHP 数组转换为 json,但我没有得到我所期望的。我希望它是一个可以使用数字索引轻松导航的对象。这是一个示例代码:
$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 :)
谢谢 :)
回答by jbafford
You want to json_encode($json, JSON_FORCE_OBJECT)
.
你想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.
JSON_FORCE_OBJECT
顾名思义,该标志强制 json 输出是一个对象,即使它通常会被表示为一个数组。
You can also eliminate the use of array_push
for some slightly cleaner code:
您还可以消除使用array_push
一些稍微干净的代码:
$json[] = ['ip' => $ip, 'port' => $port];
回答by Veshraj Joshi
just use only
只使用
$response=array();
$response["0"]=array("ip" => "192.168.0.1",
"port" => "2016");
$json=json_encode($response,JSON_FORCE_OBJECT);
回答by Cihan Uygun
To get array with objects you can create stdClass() instead of array for inner items like below;
要获取带有对象的数组,您可以创建 stdClass() 而不是像下面这样的内部项目的数组;
<?PHP
$json = array();
$itemObject = new stdClass();
$itemObject->ip = "192.168.0.1";
$itemObject->port = 2016;
array_push($json, $itemObject);
$json = json_encode($json, JSON_PRETTY_PRINT);
echo $json;
?>
A working example http://ideone.com/1QUOm6
一个工作示例http://ideone.com/1QUOm6
回答by MR_AMDEV
Just in case if you want to access your objectivitized json whole data OR a specific key value:
以防万一,如果您想访问客观化的 json 整个数据或特定键值:
PHP SIDE
PHP端
$json = json_encode($yourdata, JSON_FORCE_OBJECT);
JS SIDE
JS端
var json = <?=$json?>;
console.log(json); // {ip:"192.168.0.1", port:"2016"}
console.log(json['ip']); // 192.168.0.1
console.log(json['port']); // 2016
回答by Antonis Charalambous
In order to get an object and not just a json string try:
为了获得一个对象而不仅仅是一个 json 字符串,请尝试:
$json = json_decode(json_encode($yourArray));
If you want to jsonise the nested arrays as well do:
如果您还想对嵌套数组进行 jsonise,请执行以下操作:
$json =json_decode(json_encode($yourArray, JSON_FORCE_OBJECT));
回答by Sanju Kaniyamattam
$ip = "192.168.0.1";
$port = "2016";
$json = array("response" => array("ip" => $ip, "port" => $port));
//IF U NEED AS JSON OBJECT
$json = [array("ip" => $ip, "port" => $port)]; //IF U NEED AS JSON ARRAY
$json = json_encode($json);
echo $json;