Copy values from a PHP array

0

I have two arrays dynamic on my system:

Array 1:

$nome { [0] => nome1
        [1] => nome2
}

Array 2:

$contagem { [0] => 1
            [1] => 2
            [2] => 3
            [3] => 4
            [4] => 5
}

These two arrays , are dynamic, can have multiple input numbers, variavel name can have multiple names, and count, multiple values (up to one hundred), but always following this pattern, for reasons of I am trying to get the value of the $ variable, and that in the duplicate variable I can go each name, eg:

Array 1:

$nome { [0] => nome1
        [1] => nome2
}

Array 2:

$contagem { [0] => nome1
            [1] => nome1
            [2] => nome1
            [3] => nome1
            [4] => nome1

            [5] => nome2
            [6] => nome2
            [7] => nome2
            [8] => nome2
            [9] => nome2
}

PS: In case of doubling the variable $contagem , it was only by having 2 names in the variable $nome , if it were 3 names, it would be triple;

    
asked by anonymous 07.02.2018 / 20:39

1 answer

0

Use the array_fill function to create a array with a predefined size and value . Ex:

<?php

$nomes = ["José", "Maria"];
$contagem = [1,2,3,4,5,6,7,8,9,10];

$a = [];

foreach($nomes as $nome) {
    $arrTemp = array_fill(0, count($contagem), $nome);
    $a = array_merge($a, $arrTemp);
}

var_dump($a);

Demo

    
07.02.2018 / 21:14