Mount PHP two-dimensional array

2

I need to mount a two-dimensional array through 2 arrays received via post.

Sample arrays I get:

        Array
    (
        [0] => disc03
        [1] => disc04
    )


    Array
    (
        [0] => prof03
        [1] => prof04
    )

From the two, I need to mount the following output:

Array
(
    [0] => Array
        (
            [disciplina] => disc03
            [professor] => prof03
        )

    [1] => Array
        (
            [disciplina] => disc04
            [professor] => prof04
        )

)

Basically I need to join them where the keys are the same, but changing the value of the key to 'discipline' and 'teacher' respectively. The goal is to make multiple inserts in the database using an array.

(Use Codeigniter 3)

    
asked by anonymous 19.11.2018 / 20:21

1 answer

2

If you're going to do this in PHP, just use array_map :

$disciplinas = ['x', 'y'];
$professores = ['1', '2'];

$resultado = array_map(function ($disciplina, $professor) {
    return compact('disciplina', 'professor');
}, $disciplinas, $professores);

print_r($resultado);

The result will be:

Array
(
    [0] => Array
        (
            [disciplina] => x
            [professor] => 1
        )

    [1] => Array
        (
            [disciplina] => y
            [professor] => 2
        )

)
    
19.11.2018 / 20:28