PHP JSON structure not right

问题内容:

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:


1) Top level: AccountName(Account2)


2) 0: = Container-level


3) as you can see 1,2,3 are some data.

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 wich 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".

问题评论:

2  
Why did you post Jeff ‘s answer in your question???
    
Removed it sorry @yivi. Was to illustrate
– Asim
46 mins ago

答案:

答案1:

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;

答案评论:

    
Now none of the tags, triggers, variable are visible? see updated question added the json this outputs
– Asim
52 mins ago
1  
ah, sorry, should be $account_container->tag = count($container_version['tag']); (same for the others).
    
B-E-A-U-T-I-F-U-L @Jeff! Thanks alot :)!
– Asim
45 mins ago
    
you’re welcome!

原文地址:

https://stackoverflow.com/questions/47755469/php-json-structure-not-right

添加评论