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

152
Visualizações
Two arrays to one chunked

I have a two arrays and need to get one from them. array_merge, array_map etc. Doesn't give right result.

$array1 = [1,2,3,4,5];
$array2 = [a,b,c,d,e];

I need $array3 = [[1,a], [2,b], [3,c]]...

What is the best way to get that result?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

using array_map(null, $arr1, $arr2) you can achieve the result.
http://php.net/manual/en/function.array-map.php

php > $q = array_map(null, $array1, $array2);
php > print_r($q);
Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a
        )

    [1] => Array
        (
            [0] => 2
            [1] => b
        )

    [2] => Array
        (
            [0] => 3
            [1] => c
        )

    [3] => Array
        (
            [0] => 4
            [1] => d
        )

    [4] => Array
        (
            [0] => 5
            [1] => e
        )

)
about 4 years ago · Santiago Trujillo Relatório

0

The for loop approach is the following:

<?php
$array1 = [1,2,3,4,5];
$array2 = ['a','b','c','d','e'];
$array3 = array();
if(count($array1) == count($array2))
{
    for($i = 0; $i < count($array1); $i++)
    {
        $array3[] = [$array1[$i],$array2[$i]];
    }
}
else
{
    die("SIZE MISMATCH");
}

echo '<pre>';
print_r($array3);

And the output is:

Array
(
    [0] => Array
        (
            [0] => 1
            [1] => a
        )

    [1] => Array
        (
            [0] => 2
            [1] => b
        )

    [2] => Array
        (
            [0] => 3
            [1] => c
        )

    [3] => Array
        (
            [0] => 4
            [1] => d
        )

    [4] => Array
        (
            [0] => 5
            [1] => e
        )

)
about 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