Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

151
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda