PHP JSON 结构不对
PHP JSON structure not right
我正在开发 Google 跟踪代码管理器 API(v2)。我进行了多次 API 调用并创建了一个对象,我将其作为 JSON 发送到客户端。
我在尝试使我的 JSON 对象看起来像我想要的那样时遇到了一些困难。
我快完成了,但还没有完全完成。
为了说明,这是我现在的 JSON 对象:
{
"Account2": {
"0": {
"accountId": "1746756959",
"containerId": "7454209"
},
"1": 3,
"2": 3,
"3": 4,
"4": {
"accountId": "1746756959",
"containerId": "7519183"
},
"5": 1,
"6": 1,
"7": 2,
"not_live": "testcontainer"
},
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653"
}, 2, 1, 1],
"GTMdocx": [{
"accountId": "2120037423",
"containerId": "8031029"
}, 0, 0, 1],
"Account3": [{
"accountId": "2128191242",
"containerId": "8038449"
}, 0, 0, 0]
}
你可以看到结构如下:
1) 顶层: AccountName(Account2)
2) 0:=容器级
3) 如您所见,1、2、3 是一些数据。
我的问题是我希望下面显示的那些数字 containerid
正好位于同一对象的下方。
索引 1,2 和 3 应该在 "Account2: 0:" 下(在 containerId 下)。就像现在它显示为一个对象本身是错误的。
这是我的 PHP 代码:
static public function listAllContainers() {
$containers[] = array();
foreach (self::listAccounts()->account as $accountKey => $account) {
foreach (self::listAccountsContainers($account["path"]) as $account_container) {
try { //Because some containers might not have live-version
$container_version = self::listAccountsContainersVersion($account_container['path']);
$containers[$account['name']][] = $account_container;
$containers[$account['name']][] = count($container_version['tag']);
$containers[$account['name']][] = count($container_version['trigger']);
$containers[$account['name']][] = count($container_version['variable']);
} catch (\Exception $e) {
$containers[$account['name']]['not_live'] = $account_container['name'];
}
}
}
$filtered_array = (object)array_filter((array)$containers); // Removes empty object
return $filtered_array;
}
例如,这一行 $containers[$account['name']][] = count($container_version['tag']);
在 JSON 对象中显示为“1”,我希望它在 "account2: { 0: ....}" 中。不可能写它来说明我想要的对象 JSON:
{
"Account2": {
"0": {
"accountId": "1746756959",
"containerId": "7454209",
"tags": 3,
"triggers": 3,
"variables": 4
},
"4": {
"accountId": "1746756959",
"containerId": "7519183",
"tags": 1,
"triggers": 1,
"variables": 2,
"not_live": "testcontainer"
(....)
我已经拨打了 "Tags, Triggers, Variables"
的号码。
首先将其添加到 $account_container
!
$account_container->tag = count($container_version['tag']);
$account_container->trigger = count($container_version['trigger']);
$account_container->variable = count($container_version['variable']);
// then add the whole account to the main container
$containers[$account['name']][] = $account_container;
我正在开发 Google 跟踪代码管理器 API(v2)。我进行了多次 API 调用并创建了一个对象,我将其作为 JSON 发送到客户端。
我在尝试使我的 JSON 对象看起来像我想要的那样时遇到了一些困难。
我快完成了,但还没有完全完成。
为了说明,这是我现在的 JSON 对象:
{
"Account2": {
"0": {
"accountId": "1746756959",
"containerId": "7454209"
},
"1": 3,
"2": 3,
"3": 4,
"4": {
"accountId": "1746756959",
"containerId": "7519183"
},
"5": 1,
"6": 1,
"7": 2,
"not_live": "testcontainer"
},
"Account 1": [{
"accountId": "1766143525",
"containerId": "7483653"
}, 2, 1, 1],
"GTMdocx": [{
"accountId": "2120037423",
"containerId": "8031029"
}, 0, 0, 1],
"Account3": [{
"accountId": "2128191242",
"containerId": "8038449"
}, 0, 0, 0]
}
你可以看到结构如下:
1) 顶层: AccountName(Account2)
2) 0:=容器级
3) 如您所见,1、2、3 是一些数据。
我的问题是我希望下面显示的那些数字 containerid
正好位于同一对象的下方。
索引 1,2 和 3 应该在 "Account2: 0:" 下(在 containerId 下)。就像现在它显示为一个对象本身是错误的。
这是我的 PHP 代码:
static public function listAllContainers() {
$containers[] = array();
foreach (self::listAccounts()->account as $accountKey => $account) {
foreach (self::listAccountsContainers($account["path"]) as $account_container) {
try { //Because some containers might not have live-version
$container_version = self::listAccountsContainersVersion($account_container['path']);
$containers[$account['name']][] = $account_container;
$containers[$account['name']][] = count($container_version['tag']);
$containers[$account['name']][] = count($container_version['trigger']);
$containers[$account['name']][] = count($container_version['variable']);
} catch (\Exception $e) {
$containers[$account['name']]['not_live'] = $account_container['name'];
}
}
}
$filtered_array = (object)array_filter((array)$containers); // Removes empty object
return $filtered_array;
}
例如,这一行 $containers[$account['name']][] = count($container_version['tag']);
在 JSON 对象中显示为“1”,我希望它在 "account2: { 0: ....}" 中。不可能写它来说明我想要的对象 JSON:
{
"Account2": {
"0": {
"accountId": "1746756959",
"containerId": "7454209",
"tags": 3,
"triggers": 3,
"variables": 4
},
"4": {
"accountId": "1746756959",
"containerId": "7519183",
"tags": 1,
"triggers": 1,
"variables": 2,
"not_live": "testcontainer"
(....)
我已经拨打了 "Tags, Triggers, Variables"
的号码。
首先将其添加到 $account_container
!
$account_container->tag = count($container_version['tag']);
$account_container->trigger = count($container_version['trigger']);
$account_container->variable = count($container_version['variable']);
// then add the whole account to the main container
$containers[$account['name']][] = $account_container;