Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

182
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda