Home > Net >  Merge 2 arrays having same number of items - php
Merge 2 arrays having same number of items - php

Time:01-05

The number of items in both arrays $newResponse & $key will always be same.For each item of the $newResponse, i wanted to add items of $key to $newResponse in the same order they exists in $key array as mentioned in the Expected result.How can i achieve that?

dump($newResponse); is an array like below,which am getting as the result of a foreach loop.

array:4 [
  0 => array:6 [
    "courseId" => 18
    "courseDisplayName" => "qqq"
  ]
  1 => array:6 [
    "courseId" => 1
    "courseDisplayName" => "ips"
  ]
  2 => array:6 [
    "courseId" => 18
    "courseDisplayName" => "qqq"
  ]
  3 => array:6 [
    "courseId" => 1
    "courseDisplayName" => "ips"
  ]
]

dump($key); is an array like below, which is the result of another foreach loop.

array:4[
    0=>[
      "totalPoints" => 2
      "percent" => 1.0
      "id" => 2
    ]
    1=> [
      "totalPoints" => 10
      "percent" => 2
      "id" => 3
    ]
    2=> [
      "totalPoints" => 4
      "percent" => 0.0
      "id" => 6
    ]
    3=> [
      "totalPoints" => 4
      "percent" => 0.0
      "id" => 5
    ]
   ]

Expected result:

[
      [
        "courseId" => 18
        "courseDisplayName" => "qqq"
        "totalPoints" => 2
        "percent" => 1.0
        "id" => 2
      ]
      [
        "courseId" => 1
        "courseDisplayName" => "ips"
        "totalPoints" => 10
        "percent" => 2
        "id" => 3
      ]
      [
        "courseId" => 18
        "courseDisplayName" => "qqq"
        "totalPoints" => 4
        "percent" => 0.0
        "id" => 6
      ]
      [
        "courseId" => 1
        "courseDisplayName" => "ips"
        "totalPoints" => 4
        "percent" => 0.0
        "id" => 5
      ]
    ]

CodePudding user response:

Here's one way to do it:

Assuming your data is:

        $newResponse = [
            [
                "courseId" => 18,
                "courseDisplayName" => "qqq"
            ],
            [
                "courseId" => 1,
                "courseDisplayName" => "ips",
            ],
            [
                "courseId" => 18,
                "courseDisplayName" => "qqq",
            ],
            [
                "courseId" => 1,
                "courseDisplayName" => "ips",
            ]
        ];

        $key = [
            [
        "totalPoints" => 2,
        "percent" => 1.0,
        "id" => 2
      ],
      [
        "totalPoints" => 10,
        "percent" => 2,
        "id" => 3
      ],
      [
        "totalPoints" => 4,
        "percent" => 0.0,
        "id" => 6
      ],
      [
        "totalPoints" => 4,
        "percent" => 0.0,
        "id" => 5
      ]
    ];

Then I'd probably reach for array_map, having used it for this kind of purpose recently:

        $out = [];
        array_map(function($a,$b) use (&$out) {
            $out[] = $a   $b;
        },$newResponse,$key);
        print_r($out);

Note in the above the is very similar to array merge and I believe interchangeable in your use case. You can read about the difference here: Array_merge versus

  •  Tags:  
  • Related