I'm working on Google Tag Manager API(v2). I make multiple API calls and create an object that I send as JSON to client-side.
I'm having some difficulties trying to make my JSON object look like I want.
I'm close but not quite finished.
To illustrate, this is my JSON object now:
{
"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]
}
As you can see the structure follows like this:
My problem is that I want those numbers that are shown below containerid
to be right under in the SAME object.
Index 1,2 and 3 should be under "Account2: 0:"(under containerId). Like its now it shows as an object itself which is wrong.
This is my PHP code:
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;
}
For example, this line $containers[$account['name']][] = count($container_version['tag']);
is shown as "1" in JSON object, I want this inside "account2: { 0: ....}". Impossible to write it ill illustrate the Object I want in 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"
(....)
I have called the numbers for "Tags, Triggers, Variables"
.
Add it to the $account_container
first!
$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;