Place two Arrays inside a foreach

Asked

Viewed 1,613 times

0

Well, in case I have two arrays that are received from inputs, based on renting rooms from a hotel, in case each room needs to display number of adults and children, I got help from someone here in the stack to increase the number of rooms, but had to put below the adults the $Children array, in this case, children for each room, but this giving error here, someone knows if it is possible to do?

$adultos = array(2, 3, 2);
$criancas = array(1, 1, 3);

$i = 1;
foreach($adults as $adultos):

    echo "<strong>Quarto".$i++."</strong><br>";
    echo "Adultos:".$adultos."<br>";
    //AQUI PRECISAVA EXIBIR AS CRIANÇAS
    echo "Crianças:".$criancas."<br>";
endforeach;

I’m hitting myself here to do this, if anyone can shed a light...

  • 2

    Both arrays will have at all times the same number of elements or this is not guaranteed?

  • If not, what is the relationship between adults and children? Why do they need to be together?

  • I wouldn’t particularly iterate with foreach in this case. It seems that the data structure was not made imagining this kind of iteration. This is a frame of reference by the index, so you are most likely doomed to use the index (or convert into another data structure, using the index as possibly intermediate)

2 answers

2


If the values of $adultos and $criancas are directly related, it will be better if you group the values. It is possible to do this through the function array_map:

$adultos = array(2, 3, 2);
$criancas = array(1, 1, 3);

$quantidades = array_map(null, $adultos, $criancas);

Forming the following array:

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

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

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

)

To travel it, just use the foreach together with list:

foreach($quantidades as $i => $quantidade) {
    list($adulto, $crianca) = $quantidade;
    echo "Quarto: ", $i+1, PHP_EOL;
    echo "Adultos: ", $adulto, PHP_EOL;
    echo "Crianças: ", $crianca, PHP_EOL;
}

Generating the result:

Quarto: 1
Adultos: 2
Crianças: 1

Quarto: 2
Adultos: 3
Crianças: 1

Quarto: 3
Adultos: 2
Crianças: 3

Which is to be expected.

See working on Ideone.

  • in reality they will always be different, the values, because the user will fill in a form the data, whether have child or not, how many adults, and an input "infinite" for each room, this input "infinite" that generates the array.

  • Can you explain it better? So this solution does not solve the problem? Why?

  • ah yes, now it worked, I had not declared the variable "$quantity" but it worked now, thank you again Anderson! Have a great week, buddy!

0

The code is commented to be able to understand.

$adultos = array(2, 3, 2);
$criancas = array(1, 1, 3);

$i = 0; // Altere o valor da variável $i para 0
foreach($adultos as $adultos): // Corrija o nome da variável $adultos pois está $adults
    echo "<strong>Quarto ".($i+1)."</strong>\n"; // ($i+1) soma o valor de $i com mais 1, apenas para exibição.
    echo "Adultos:".$adultos."\n";
    //AQUI PRECISAVA EXIBIR AS CRIANÇAS
    // O valor da variável $i vai ser a chave para retornar a quantidade de crianças
    echo "Crianças:".$criancas[$i]."\n\n"; // Exibe a quantidade de crianças
    $i++; // Incrementa
endforeach;

You can see it working on ideone.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.