I need to use for each
with 4 arrays for a school project but I couldn't find a solution somewhere. Is there a way to use for each
loop using 4 arrays
. I tried multiple things but nothing worked for me.
<?php
$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");
$persoon = array_rand($voornaam, 1);
foreach ($Voornamen as $Voornaam and $Achternamen as $Achternaam and $Woonplaatsen as $Woonplaats and $Leeftijden as $Leeftijd){
echo "<table>
<tr>
<td>
$Voornaam
</td>
</tr>
<tr>
<td>
$Achternaam
</td>
</tr>
<tr>
<td>
$Woonplaats
</td>
</tr>
<tr>
<td>
$Leeftijd
</td>
</tr>
</table>";
}
?>
Changing the data structure would be a good solution but if you have to stick with that you can use this code
foreach ($Voornamen as $id => $value){
echo "<table>
<tr>
<td>
".$value."
</td>
</tr>
<tr>
<td>
" . ($Achternamen[$id]) . "
</td>
</tr>
<tr>
<td>
".($Woonplaatsen[$id])."
</td>
</tr>
<tr>
<td>
".($Leeftijden[$id])."
</td>
</tr>
</table>";
}
You can loop through each of them one by one:
foreach($array1 as $key1 => $value1){
foreach($array2 as $key2 => $value2){
... etc
But this can get rather messy rather quick. Do you really have to use 4 arrays?
I tried your all your array into one array and create a function to show all the names here is the code:
$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");
//$persoon = array_rand($voornaam, 1);
$arr = array($Voornamen,$Achternamen,$Woonplaatsen,$Leeftijden);
multi_array($arr);
function multi_array($array){
foreach ($array as $key => $val){
foreach($val as $v) {
echo "<table>
<tr>
<td>
$v
</td>
</tr>
</table>";
}
}
}
Use foreach
to iterate on array; print the value and use the key with the square brackets syntax to access the corresponding values from the other arrays:
$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");
$persoon = array_rand($voornaam, 1);
foreach ($Voornamen as $id => $Voornaam) {
$Achternaam = $Achternamen[$id];
$Woonplaats = $Woonplaatsen[$id];
$Leeftijd = $Leeftijden[$id];
?>
<table>
<tr>
<td><?php echo(htmlspecialchars($Voornaam)); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($Achternaam)); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($Woonplaats)); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($Leeftijd)); ?></td>
</tr>
</table>
<?php } ?>
A better way is to keep all properties of the same person in a single associative array and keep only only list that includes all the persons indexed by their IDs:
$persons = array(
'0300013' => array(
'voornaam' => 'Laurence',
'achternaam' => 'Van der Wel',
'woonplaats' => 'Enschede',
'leeftijd' => '17',
),
'0266931' => array(
'voornaam' => 'Peter',
'achternaam' => 'Kuipers',
'woonplaats' => 'Enschede',
'leeftijd' => '20',
),
'0267730' => array(
'voornaam' => 'Pim',
'achternaam' => 'Hoomans',
'woonplaats' => 'Enschede',
'leeftijd' => '20',
),
'0279410' => array(
'voornaam' => 'Arben',
'achternaam' => 'Hajrizaj',
'woonplaats' => 'Enschede',
'leeftijd' => '19',
),
'0297652' => array(
'voornaam' => 'Robbin',
'achternaam' => 'Visser',
'woonplaats' => 'Overdinkel',
'leeftijd' => '16',
),
'0300838' => array(
'voornaam' => 'Damian',
'achternaam' => 'Trojak',
'woonplaats' => 'Enschede',
'leeftijd' => '16',
),
);
foreach ($persons as $p) { ?>
<table>
<tr>
<td><?php echo(htmlspecialchars($p['voornaam'])); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($p['achternaam'])); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($p['woonplaats'])); ?></td>
</tr>
<tr>
<td><?php echo(htmlspecialchars($p['leeftijd'])); ?></td>
</tr>
</table>
<?php } ?>
It's not clear of the final intent so I presumed the end goal was to display the data from the arrays in a table?
As mentioned you can only supply one array to foreach
but you could, as the source arrays are of the same lengths, use a counter to allow access to other arrays.
$Voornamen = array( '0300013' => "Laurence" , '0266931' => "Peter" , '0267730' => "Pim" , '0279410' => "Arben" , '0297652' => "Robbin" , '0300838' => "Damian");
$Achternamen = array( '0300013' => "Van der Wel" , '0266931' => "Kuipers" , '0267730' => "Hoomans" , '0279410' => "Hajrizaj" , '0297652' => "Visser" , '0300838' => "Trojak");
$Woonplaatsen = array( '0300013' => "Enschede" , '0266931' => "Enschede" , '0267730' => "Enschede" , '0279410' => "Enschede" , '0297652' => "Overdinkel" , '0300838' => "Enschede");
$Leeftijden = array( '0300013' => "17" , '0266931' => "20" , '0267730' => "20" , '0279410' => "19" , '0297652' => "16" , '0300838' => "16");
$kv=array_keys( $Voornamen );
$ka=array_keys( $Achternamen );
$kw=array_keys( $Woonplaatsen );
$kl=array_keys( $Leeftijden );
$i=0;
echo "<table cellspacing='5px' cellpadding='10px' border='1'>";
foreach( $Voornamen as $key => $value ){
echo "
<tr>
<td>{$key}:{$value}</td>
<td>{$ka[ $i ]}:{$Achternamen[ $ka[ $i ] ]}</td>
<td>{$kw[ $i ]}:{$Woonplaatsen[ $kw[ $i ] ]}</td>
<td>{$kl[ $i ]}:{$Leeftijden[ $kl[ $i ] ]}</td>
</tr>";
$i++;
}
echo "</table>";
Yes it is possible since all arrays have the same keys and size:
foreach ($Voornamen as $key => $value){
echo $Voornamen[$key];// or just $value
echo $Achternamen[$key];
echo $Woonplaatsen[$key];
echo $Leeftijden[$key];
}
Another way is to construct a multidimensional array from all the arrays and use two nested FOREACH loop.
............
My greetings to your teacher.