Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

183
Views
Merge 2 arrays having same number of items - php

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
      ]
    ]
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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 +

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!