PHP中的JSON对象插入
在JSON对象中正确插入一些数据时,我遇到了一些问题。我希望JSON对象的顶部没有任何键。下面是我的JSON: JSON现在:PHP中的JSON对象插入,php,arrays,json,object,Php,Arrays,Json,Object,在JSON对象中正确插入一些数据时,我遇到了一些问题。我希望JSON对象的顶部没有任何键。下面是我的JSON: JSON现在: { "Account2": [{ "accountId": "", "containerId": "", "tag": 1, "trigger": 1, "variable": 4, "account_name": "Account2" }, {
{
"Account2": [{
"accountId": "",
"containerId": "",
"tag": 1,
"trigger": 1,
"variable": 4,
"account_name": "Account2"
}, {
"accountId": "",
"containerId": "",
"missing_live": true
}],
"Account 1": [{
"accountId": "",
"containerId": "",
"tag": 2,
"trigger": 1,
"variable": 1,
"account_name": "Account 1"
}],
"GTMdocx": [{
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 1,
"account_name": "GTMdocx"
}]
}
如您所见,Account2、Account1、GTMdocx等。。它们就像json对象中的帐户名。我想删除顶键并保留帐户名。但是当我删除代码中的行时,我只得到一个responseone JSON对象,而不是全部3个
PHP代码:
try { // Some containers might not have live-version
$container_version = self::listAccountsContainersVersion($account_container['path']);
$account_container->tag = count($container_version['tag']);
$account_container->trigger = count($container_version['trigger']);
$account_container->variable = count($container_version['variable']);
$account_container->account_name = $account['name'];
// Add the whole account to the main container
$containers[$account['name']][] = $account_container;
} catch (\Exception $e) {
$account_container->missing_live = true;
$containers[$account['name']][] = $account_container;
//$containers[$account['name']]['missing_live_version'] = $account_container; //Container not published
}
我评论//将整个帐户添加到主容器中。。。这是所有人都会出错的地方。我试着把它改成:
$containers[$account] = $account_container;
因此,我将获得$account_容器中包含的所有$account JSON对象,但如果我没有$account['name'],则当其值为3时,响应仅显示一个对象。这在foreach循环中
所以我希望一个JSON对象是这样的:
{
"1": {
"accountId": "",
"containerId": "",
"tag": 1,
"trigger": 1,
"variable": 4,
"account_name": "Account2"
},
"2": {
"accountId": "",
"containerId": "",
"missing_live": true,
"account_name": "Account2"
},
"3": {
"accountId": "",
"containerId": "",
"tag": 2,
"trigger": 1,
"variable": 1,
"account_name": "Account 1"
},
"4": {
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 1,
"account_name": "GTMdocx"
},
"5": {
"accountId": "",
"containerId": "",
"tag": 0,
"trigger": 0,
"variable": 0,
"account_name": "Account3"
}
}
你可以看到我删除了上面的Account2 因此,要获得所需的JSON字符串,您需要一个维度数组 所以像这样保存您的帐户\u容器
$containers[] = $account_container;
输入非法偏移量…很漂亮。非常感谢。