'PHP Multi Array Merging JSON

I've got JSON output that comes to me like this

{ [objects{}] [to] [from] [total] }

and due to the API I'm pulling this from I can only pull 500 records at a time, what I'm trying to do is merge the objects part of multiple json outputs into a larger recordset.

I've tried using array_merge with json_encode and json_decode but it's not working, I'm assuming it's because of the other arrays (to/from/total) that are impacting my ability to merge.

Any idea how I could go about this, I was thinking of a line by line merge, but issue with this is different types of arrays have different elements (I'm only trying to merge arrays with the same elements).

Edit: hmmm json formatting didn't come through well even with code tags

{
  "objects" : [ {
    "uid" : "f7534a54-fd17-4bc2-9a26-8a567082cc86",
    "name" : "host_10.1.1.1",
    "type" : "host",
    "domain" : {
      "uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
      "name" : "Lab-Ext_VPN",
      "domain-type" : "domain"
    },
    "ipv4-address" : "10.1.1.1"
  }, {
    "uid" : "1ba63e41-4d2c-48ea-a970-66a806089ff5",
    "name" : "host_10.1.1.10",
    "type" : "host",
    "domain" : {
      "uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
      "name" : "Lab-Ext_VPN",
      "domain-type" : "domain"
    },
    "ipv4-address" : "10.1.1.10"
  }, {
    "uid" : "d798b126-25de-4138-b25d-b1edfe83d259",
    "name" : "host_10.1.1.100",
    "type" : "host",
    "domain" : {
      "uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
      "name" : "Lab-Ext_VPN",
      "domain-type" : "domain"
    },
    "ipv4-address" : "10.1.1.100"
  }, {
    "uid" : "247e1b2e-a45d-457e-87f7-78a6acdeea9f",
    "name" : "host_10.1.1.101",
    "type" : "host",
    "domain" : {
      "uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
      "name" : "Lab-Ext_VPN",
      "domain-type" : "domain"
    },
    "ipv4-address" : "10.1.1.101"
  }, {
    "uid" : "381e59a0-2094-422a-b538-8f607ec58384",
    "name" : "host_10.1.1.102",
    "type" : "host",
    "domain" : {
      "uid" : "8fe7e918-8530-4a7d-851c-bf8a7c3889c9",
      "name" : "Lab-Ext_VPN",
      "domain-type" : "domain"
    },
    "ipv4-address" : "10.1.1.102"
  } ],
  "from" : 1,
  "to" : 5,
  "total" : 614
`


Solution 1:[1]

You have 4 elements in the given json, object to from and total

I am assuming, you hav to merge the object from two JSONs, get minimum value of from, get maximum value from to and sum of total

function my_merge($json_1,$json_2){
    $array_1 = json_decode($json_1,true);
    $array_2 = json_decode($json_2,true);
    return ['objects'=>array_merge($array_1['objects'],$array_2['objects']),'from'=>min($array_1['from'],$array_2['from']),'to'=>max($array_1['to'],$array_2['to']),'total'=>($array_1['total']+$array_2['total'])];
}

Above, first, json_decode with second parameter true to decode and convert JSON to array.

Second, merge the index [0] of both $array_1 and $array_2 as it contains the object. Do the min max and summation for total and return the resultant array.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1