Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typo3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
大括号周围的PHP方括号_Php_Arrays_Json - Fatal编程技术网

大括号周围的PHP方括号

大括号周围的PHP方括号,php,arrays,json,Php,Arrays,Json,我的数组和json_编码有问题。我希望它看起来像这样 "invites": [{ "sent": 0, "accepted": 0 }], 但看起来是这样的 "shares": { "sent": 0, "installed": 0 }, 'shares' => array( 'sent' => 0, 'installed' => 0 ), 目前我使用的代码如下所示 "shares": { "sent": 0, "installed": 0

我的数组和json_编码有问题。我希望它看起来像这样

"invites": [{
  "sent": 0,
  "accepted": 0
}],
但看起来是这样的

"shares": {
  "sent": 0,
  "installed": 0
},
'shares' => array(
  'sent' => 0,
  'installed' => 0
),
目前我使用的代码如下所示

"shares": {
  "sent": 0,
  "installed": 0
},
'shares' => array(
  'sent' => 0,
  'installed' => 0
),
有人知道我会如何改变这一点吗。 谢谢 Matt

您必须将(关联)数组包装到另一个数组中:

'shares' => array(array(
   'sent' => 0,
   'installed' => 0,
))

php中的关联数组转换为javascript中的json对象

你想要:

array(
    'shares' => array(
         array('sent' => 0, 'installed' => 0)
     )
)

有什么问题?当您解析JSON时,您得到的JSON将被转换回关联数组。在从JSON解析结果对象之后,您是否尝试过对其执行
var\u dump
?JSON不是javascript。这看起来像
['shares':{'sent':0,'installed':0}]
。他想要
'shares':[{'sent':0,'installed':0}]
在数组外部使用
'shares'
。但是你的思路是对的。@DutGRIFF:答案正确吗?或者输出完全正确吗?@DutGRIFF OP说的是使用json_encode()对数组进行编码。。。如果不是最外层阵列的密钥,“共享”是什么?@zerkms我不明白你的问题。我想Jason的意思是把数组放在关联数组的周围。OP在帖子中遗漏的
'shares'
周围有一个数组。@DutGRIFF:这个答案中的代码将完全按照OP的要求生成结果,这就是我的观点。