【问题标题】:Merging JSON while keeping key layout在保持键布局的同时合并 JSON
【发布时间】:2020-05-27 06:05:04
【问题描述】:

我正在尝试将从 API 获得的多个 JSON 结果合并为一个。

我收到的 JSON 数据如下所示:

{
  "total": 100,
  "offset": 0,
  "articleCollection": [
    {
      "artikelindeling": {
        "artikelgroep": "string",
        "artikelgroep_omschrijving": "string",
        "kernwoord": "string",
        "kernwoord_omschrijving": "string",
        "klantgroep": "string",
        "klantgroep_omschrijving": "string",
        "plaatsbepaling": "string",
        "plaatsbepaling_omschrijving": "string",
        "unieknummerserie": "string"
      },
      "artikelinformatie": {
        "prijsinformatie": {
          "actie": {
            "actie_bruto_inkoopprijs": 0,
            "actie_consumentenadviesprijs": 0,
            "actie_datum_tot_en_met": "2020-02-11T18:19:51.747Z",
            "actie_datum_vanaf": "2020-02-11T18:19:51.747Z"
          },
          "basisprijs_dealer": 0,
          "btw": "string",
          "btw_omschrijving": "string",
          "consumentenadviesprijs": 0
        }
      },
      "dateModified": "2020-02-11T18:19:51.747Z",
      "date_modified": "string",
      "land": "string",
      "taal": "string",
      "valuta": "string"
    }
  ]
}

这是我目前使用的代码:

        $username = $_POST['username'];
        $password = $_POST['password'];
        $leverancier = $_POST['leverancier'];
        $offset = 0;

        $jsonCombined = array();
        $data = call_curl($username, $password, $leverancier, $offset); //First call needed to get total amount
        $jsonArr = json_decode($data, true);

        if ($jsonArr['total'] > 50) { //API limits to 50 results per call, need multiple calls.
            $totalCalls = floor($jsonArr['total'] / 50); //Calculate how many calls are needed.
            foreach($jsonArr as $record) {
                $jsonCombined[] = $record;
            }
        for ($i = 0; $i < $totalCalls; $i++) {
            $offset += 50;
            $data = call_curl($username, $password, $leverancier, $offset);
            $jsonArr = json_decode($data, true);
            foreach($jsonArr as $record) {  
                $jsonCombined[] = $record;
            }
        }       
        }
        else {
            foreach($jsonArr as $record) {
                $jsonCombined[] = $record;
            }
        }

期望的结果是:

Array ( [total] => 100 [offset] => 0 [articleCollection] => Array ( [0] => Array() [1] => Array() [2] => Array()))

但是,我收到了:

Array ( [0] => 100 [1] => 0 [2] => Array ( [0] => Array() [1] => Array() [2] => Array()))

我将如何进行更改以使密钥保持不变并将其他项目添加到“articleCollection”中?

【问题讨论】:

    标签: php arrays json merge


    【解决方案1】:

    由于很难正确测试,这段代码是我能猜到的。

    问题在于,当您使用$jsonCombined[] 添加数据时,这只会以数字顺序将数据添加到数组的末尾。这段代码取而代之的是获取密钥以及行中的内容,例如...

    foreach($jsonArr as $key => $record)
    

    然后用这个key加上key的数据...

    $jsonCombined[$key] = $record;
    

    获取数据页面的内部循环,只复制"articleCollection"元素,这次使用$jsonCombined["articleCollection"][]继续添加数据(这个位是我不确定这是否工作正常的部分.)

    if ($jsonArr['total'] > 50) { //API limits to 50 results per call, need multiple calls.
        $totalCalls = floor($jsonArr['total'] / 50); //Calculate how many calls are needed.
        foreach($jsonArr as $key => $record) {
            $jsonCombined[$key] = $record;
        }
        for ($i = 0; $i < $totalCalls; $i++) {
            $offset += 50;
           $data = call_curl($username, $password, $leverancier, $offset);
            $jsonArr = json_decode($data, true);
            foreach($jsonArr["articleCollection"] as $record) {
                $jsonCombined["articleCollection"][] = $record;
            }
        }
    }
    else {
        foreach($jsonArr as $key => $record) {
            $jsonCombined[$key] = $record;
        }
    }
    

    最后一个foreach 可能不需要,因为它只是将数据从$jsonArr 复制到$jsonCombined。所以你可以把一个分配给另一个。

    【讨论】:

    • 非常感谢!就像我想要的那样工作。没想到会这样。我也更改了最后一个 foreach,因为它确实不需要。
    【解决方案2】:

    您可以提前准备好“主”数组,然后将值填充到其中。

      $jsonCombined = array(
        'total' => 0, 
        'offset' => 0, 
        'articleCollection' => array()
      );
    

    现在对于每次迭代,您都可以将值推入其中。

      $jsonArr = json_decode($data, true);
      foreach($jsonArr["articleCollection"] as $item) {
        $jsonCombined["articleCollection"][] = $item;
      }
    

    希望对你有帮助

    【讨论】:

    • 感谢您的帮助。这也有效,但我选择了另一个答案,所以我没有准备我的数组。
    • @WJCra 感谢您的反馈!有不同的风格和口味来调味你的汤。最重要的是你的问题得到了回答,无论你选择哪种方式:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-10
    相关资源
    最近更新 更多