dstxpei5823 2017-04-27 06:44
浏览 45
已采纳

使用PHP创建带动态密钥的嵌套JSON

I get some data from an API call and I try to construct a JSON to send it back to another API.

The final JSON must look like this:

{
    "jobs": {
        "MP_OFFER_ID_1": {
            "job_id": "jobboard_reference_1",
            "url": "url_1",    
            "status": 0
        },
        "MP_OFFER_ID_2": {
            "job_id": "job_id_2",
            "url": "url_2",
            "status": 1
        },
    }
}

So under the jobs key, it's not an array of elements but a list of elements with unique keys.

And that's what I'm having trouble to get.

The data I want to parse is in an array structured like this:

Array(
    [0] => Array(
        [link] => some-url
        [id] => 18790674
        [ref] => 0015909-18790674
    )
    // ...
);

link has to be placed in the url key of the JSON.

id is the JSON key, in the examples it's MP_OFFER_ID_1 etc

ref has to be placed in job_id

I actually have this JSON at the end:

{
    "jobs": [
        [
            {
                "18790674": {
                    "job_id": "0015909-18790674",
                    "url": "test",
                    "status": 1
                }
            },
            {
                "18790678": {
                    "job_id": "0015892-18790678",
                    "url": "test",
                    "status": 1
                }
            }
        ]
    ]
}

As you can see, jobs is an array (actually it's an array of array ^^) and that's not what I want here...

展开全部

  • 写回答

1条回答 默认 最新

  • douxiapi4381 2017-04-27 07:11
    关注

    I came up with this, but it was very difficult to understand what exactly you want from the very limited info in question:

    <?php
    $input = [
      [
        'id' => 18790674,
        'link' => 'some-url',
        'ref' => '0015909-18790674',
      ],
      [
        'id' => 18790678,
        'link' => 'another-url',
        'ref' => '0015909-18790678',
      ],
    ];
    
    $output = new stdClass();
    
    foreach ($input as $arr) {
      $obj = new stdClass();
    
      $key = $arr['id'];
      $obj->job_id = $arr['id'];
      $obj->url = $arr['link'];
      $obj->status = '1'; // ?
    
      $output->{$key} = $obj;
    
    }
    
    echo json_encode($output, JSON_PRETTY_PRINT);
    

    Output:

    {
        "18790674": {
            "job_id": 18790674,
            "url": "some-url",
            "status": "1"
        },
        "18790678": {
            "job_id": 18790678,
            "url": "another-url",
            "status": "1"
        }
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 torch框架下的强化学习DQN训练奖励值浮动过低,希望指导如何调整
  • ¥35 西门子博图v16安装密钥提示CryptAcquireContext MS_DEF_PROV Error of containger opening
  • ¥15 mes系统扫码追溯功能
  • ¥40 selenium访问信用中国
  • ¥20 在搭建fabric网络过程中遇到“无法使用新的生命周期”的报错
  • ¥15 Python中关于代码运行报错的问题
  • ¥500 python 的API,有酬谢
  • ¥15 软件冲突问题,软件残留问题
  • ¥30 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
  • ¥50 有没有人会写hLDA,有偿求写,我有一个文档,想通过hLDA得出这个文档的层次主题,有偿有偿!
手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部