Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在保持键布局的同时合并JSON_Php_Arrays_Json_Merge - Fatal编程技术网

Php 在保持键布局的同时合并JSON

Php 在保持键布局的同时合并JSON,php,arrays,json,merge,Php,Arrays,Json,Merge,我正在尝试将从API获得的多个JSON结果合并到一个结果中 我收到的JSON数据如下所示: { "total": 100, "offset": 0, "articleCollection": [ { "artikelindeling": { "artikelgroep": "string", "artikelgroep_omschrijving": "string", "kernwoord": "string",

我正在尝试将从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 ( [0] => 100 [1] => 0 [2] => Array ( [0] => Array() [1] => Array() [2] => Array()))

我该如何更改此项,使键保持不变,并将附加项添加到“articleCollection”中

因为很难正确测试,所以我猜这段代码非常接近

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

foreach($jsonArr as $key => $record)
然后使用此键添加具有该键的数据

$jsonCombined[$key] = $record;
获取数据页面的内部循环只复制
“articleCollection”
元素,这次使用
$jsonCombined[“articleCollection”][/code>继续添加数据(我不确定这一部分是否正常)

if($jsonArr['total']>50){//API将每个调用的结果限制为50个,需要多个调用。
$totalCalls=floor($jsonArr['total']/50);//计算需要多少调用。
foreach($jsonarras$key=>$record){
$jsonCombined[$key]=$record;
}
对于($i=0;$i<$totalCalls;$i++){
$offset+=50;
$data=call_curl($username、$password、$leverancier、$offset);
$jsonArr=json_decode($data,true);
foreach($jsonArr[“articleCollection”]作为$record){
$jsonCombined[“articleCollection”][]=$record;
}
}
}
否则{
foreach($jsonarras$key=>$record){
$jsonCombined[$key]=$record;
}
}
最后一个
foreach
可能不需要,因为它只是将数据从
$jsonArr
复制到
$jsoncomponed
。因此,您可以将一个数组分配给另一个数组。

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

  $jsonCombined = array(
    'total' => 0, 
    'offset' => 0, 
    'articleCollection' => array()
  );
  $jsonArr = json_decode($data, true);
  foreach($jsonArr["articleCollection"] as $item) {
    $jsonCombined["articleCollection"][] = $item;
  }
现在,对于每个迭代,您可以将值推送到其中

  $jsonCombined = array(
    'total' => 0, 
    'offset' => 0, 
    'articleCollection' => array()
  );
  $jsonArr = json_decode($data, true);
  foreach($jsonArr["articleCollection"] as $item) {
    $jsonCombined["articleCollection"][] = $item;
  }

希望有帮助

谢谢你的帮助。这同样有效,但我使用了另一个答案,因此我没有准备数组。@WJCra感谢您的反馈!有不同的风格和口味来给你的汤调味。最重要的是,你的问题得到了回答,你选择哪种方式是独立的:-)非常感谢!工作就像我想要的那样。没想到会这样。我也更改了最后一个foreach,因为它确实不需要。