dongze8698 2017-12-11 14:50
浏览 39
已采纳

PHP JSON结构不对

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

  • 写回答

1条回答 默认 最新

  • dsfsdf7852 2017-12-11 15:05
    关注

    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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 用友U8:向一个无法连接的网络尝试了一个套接字操作,如何解决?
  • ¥30 我的代码按理说完成了模型的搭建、训练、验证测试等工作(标签-网络|关键词-变化检测)
  • ¥50 mac mini外接显示器 画质字体模糊
  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C