JSON结构PHP - 奇怪

时间:2017-12-07 08:54:53

标签: php arrays json

我正在使用Google标记管理器API(v2),并尝试向现有的JSON数组添加一些新行。我首先在向JSON对象添加数据时遇到了一些麻烦,因为数据显示在最后一行而不是对象本身内。

现在我已经改变了我的代码,但数据看起来并不像它在对象本身内部,但现在我更接近了。这就是我的JSON对象现在的样子:

JSON:

{
    "Account2": [{
        "accountId": "17467*****",
        "containerId": "745****",
    }, 3, 3, 4, {
        "accountId": "17467*****",
        "containerId": "751****",
    }, 1, 1, 2],
    "Account 1": [{
        "accountId": "17661*****",
        "containerId": "748****",
    }, 2, 1, 1],
    "GTMdocx": [{
        "accountId": "21200*****",
        "containerId": "803****",
    }, 0, 0, 1],
    "Account3": [{
        "accountId": "21281*****",
        "containerId": "803****",
    }, 0, 0, 0]
}

正如您所见,对象的结构如下:
1)帐户名
2)在Accountname内,我们有accountIdcontainerId
3)在一个Accountname对象的最后我们有一些数字(计数),问题是它看起来像这样: enter image description here

我希望看起来像JSON对象是这样的:

{
    "Account2": [{
        "accountId": "17467*****",
        "containerId": "745****",
        "tags": "3",
        "triggers": "3",
        "variables": "4"

    }, {
        "accountId": "17467*****",
        "containerId": "751****",
        "tags": "3",
        "triggers": "3",
        "variables": "4"
    }],
    "Account 1": [{
        "accountId": "17661*****",
        "containerId": "748****",
        "tags": "2",
        "triggers": "1",
        "variables": "1"
    }],
    "GTMdocx": [{
        "accountId": "21200*****",
        "containerId": "803****",
        "tags": "0",
        "triggers": "0",
        "variables": "1"
    }],
    "Account3": [{
        "accountId": "21281*****",
        "containerId": "803****",
        "tags": "0",
        "triggers": "0",
        "variables": "0"
    }]
}

您可以看到数字有一个键,然后是对象内的值。

这是我的PHP代码:

static public function listAllContainers() {

    try { //Because some containers might not have live-version
        $containers[] = array();
        foreach (self::listAccounts()->account as $accountKey => $account) {
            foreach (self::listAccountsContainers($account["path"]) as $account_container) {
                $containers[$account['name']][] = $account_container;

                $container_version = self::listAccountsContainersVersion($account_container['path']);
                $containers[$account['name']][] = count($container_version['tag']);
                $containers[$account['name']][] = count($container_version['trigger']);
                $containers[$account['name']][] = count($container_version['variable']);


            }
        }
        $filtered_array = (object)array_filter((array)$containers); // Removes empty object
        return $filtered_array;
    } catch (\Exception $e) {
        return $e->getCode();
    }
}

希望你理解我的问题。与Arrays / JSON没那么多经验所以也许我的解释不太好。 谢谢!

更新
使用$containers[$account['name']]['tag'] = count($container_version['tag']);给了我这个JSON :( PS:我从json中移除了一些因此它不会很长,这会使json无效)

{
Account2: {
0: {
accountId: "174675****",
containerId: "745****",
},
1: {
accountId: "174675****59",
containerId: "751****83",
},
tag: 1,
trigger: 1,
variable: 2
},
Account 1: {
0: {
accountId: "1766***525",
containerId: "748***53",
},
tag: 2,
trigger: 1,
variable: 1
},

现在每个“帐户”都可以有多个“容器”(accountId,containerId),使用此方法不会将数据存储在对象本身内。

2 个答案:

答案 0 :(得分:1)

如果您有一个数组,可以使用以下语法为其添加一个键:值对:

$array['key'] = 'value';
//array('key' : 'value')

如果您没有指定密钥,则只需添加值:

$array[] = 'value';
//array('value')

最后一部分是您现在正在做的事情,但是您将它添加到$ containers [...]数组中。你想要做的是首先将值添加到$ account_container数组,然后将该数组添加到$ containers [...]数组中。把它放在内在的foreach:

        $container_version = self::listAccountsContainersVersion($account_container['path']);

        //Add values to account_container
        $account_container['tag'] = count($container_version['tag']);
        $account_container['trigger'] = count($container_version['trigger']);
        $account_container['variable'] = count($container_version['variable']);

        // Add account_container to your $containers[..] array
        $containers[$account['name']][] = $account_container;

答案 1 :(得分:0)

我在不修改原始$account_container的情况下将其他解决方案放在一起。

我已将新数据添加到新数组$account_container_extra中,并在将其附加到$account_container数组时将其与$container合并。

也许这对您更有效,或者给您一些其他想法。

static public function listAllContainers() {

    try { //Because some containers might not have live-version
        $containers[] = array();
        foreach (self::listAccounts()->account as $accountKey => $account) {
            foreach (self::listAccountsContainers($account["path"]) as $account_container) {
                $container_version = self::listAccountsContainersVersion($account_container['path']);

                $account_container_extra = [
                    'tag' => count($container_version['tag']),
                    'trigger' => count($container_version['trigger']),
                    'variable' => count($container_version['variable'])
                ];

                $containers[$account['name']][] = array_merge($account_container, $account_container_extra);
            }
        }
        $filtered_array = (object)array_filter((array)$containers); // Removes empty object
        return $filtered_array;
    } catch (\Exception $e) {
        return $e->getCode();
    }
}