dstxpei5823
dstxpei5823
2017-04-27 14: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...

图片转代码服务由CSDN问答提供 功能建议

我从API调用中获取了一些数据,并尝试构建一个JSON以将其发送回另一个API。 / p>

最终的JSON必须如下所示:

  {
“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”,
“状态”:1 
},
} 
} 
   
 
 

所以在 jobs 下 key,它不是一个元素数组,而是一个带有唯一键的元素列表。

这就是我遇到的问题。

我要解析的数据是这样构造的数组:

  Array(
 [0] =>数组(
 [link] => some-  url 
 [id] => 18790674 
 [ref] => 0015909-18790674 
)
 // ... 
); 
   
 
 <  p>  link 必须放在中 JSON的url 键。 
 
 

id 是JSON键,在示例中它是 MP_OFFER_ID_1

ref 必须放在 job_id

我实际上最后有这个JSON:< / p>

  {
“jobs”:[
 [
 {
“18790674”:{
“job_id”:“0015909-18790674”,
“url  “:”test“,
”status“:1 
} 
},
 {
”18790678“:{
”job_id“:”0015892-18790678“,
”url“:”test  “,
”状态“:1 
} 
} 
] 
] 
} 
   
 
 

如您所见, jobs 是一个数组(实际上它是一个数组^^的数组),这不是我想要的......

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • douxiapi4381
    douxiapi4381 2017-04-27 15: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"
        }
    }
    
    点赞 评论

相关推荐