oliverbj
oliverbj

Reputation: 6052

Combine two arrays two one and set keys

I have been struggling with this for quite some time. I have two arrays, which I need to combine.

This is my input:

{
    "array_one": {
        "mrnreference": [
            {
              "key_0": "18DK00310020B11A84"
            },
            {
              "key_0": "18DK00310020B11B40"
            }
          ]
    },
    "array_two": {
        "shipperreference": [
            {
              "key_0": "1861575"
            },
            {
              "key_0": "1861549"
            }
          ]
    }
}

Now the structure is, that each item in each array follows each other. So, the result should be something like:

{
  "result": [
    {
       "mrn"     : "18DK00310020B11A84",
       "shipper" : "1861575"
    },
    {
       "mrn"     : "18DK00310020B11B40",
       "shipper" : "1861549"
    }
  ]
}

However I simply cannot figure out how to do this.

I have tried to merge the two original arrays:

//Input
$array_one = $request->array_one;
$array_two = $request->array_two;

//Merge the two received arrays
$final = array_merge_recursive($array_one, $array_two);

However, this just removes array_one and array_two, but the array is still split up.

How can I combine above array, so it will have below format:

{
     "mrn"     : "18DK00310020B11B40",
     "shipper" : "1861549"
}

Upvotes: 2

Views: 77

Answers (3)

cmprogram
cmprogram

Reputation: 1884

A very quick solution to this would be just to iterate through a for loop.

for($i = 0; $i < count($array_one); $i++){
    $final[$i]["mrn"] = $array_one["mrnreference"][$i]; // Mrn key equals array one value
    $final[$i]["shipping"] = $array_two["shipperreference"][$i]; // Shipping key equals array two value
}

However, this has a small caveat that it could lead to an error, if $array_one and $array_two are not the same size.

Upvotes: 1

apokryfos
apokryfos

Reputation: 40653

You can do this with some custom code:

$array_one = $request->array_one;
$array_two = $request->array_two;

$final = array_map(function ($value, $key) use ($array_two) {
    foreach ($value as $k => $v) {
        return [
            "mrn" => $v,
            "shipper" => array_get($array_two, "shipperreference.$key.$k")
        ];
    }
}, array_get($array_one, 'mrnreference'), array_keys(array_get($array_one, 'mrnreference')));

Upvotes: 2

Robert
Robert

Reputation: 20286

First of all array_map can be used to get the values and then in simple for loop you can combine them. Notice that the size of mrnreference and shipperreference must be the same otherwise it will pop notice

$json = '
{
    "array_one": {
        "mrnreference": [
            {
              "key_0": "18DK00310020B11A84"
            },
            {
              "key_0": "18DK00310020B11B40"
            }
          ]
    },
    "array_two": {
        "shipperreference": [
            {
              "key_0": "1861575"
            },
            {
              "key_0": "1861549"
            }
          ]
    }
}
';
$arr = json_decode($json, true);
$ref = array_map(function($e){return $e['key_0'];}, $arr['array_one']['mrnreference']);
$ship = array_map(function($e){return $e['key_0'];}, $arr['array_two']['shipperreference']);



$output = array();

for ($i = 0, $cnt = count($ref); $i < $cnt ; ++$i) {
  $output[] = [
     'mrn' => $ref[$i],
     'shipper' => $ship[$i],
  ];
}


echo json_encode(['result' => $output]);

Upvotes: 0

Related Questions