Reputation: 1969
I'm trying to display only the names that are not empty from this array on a new line for each. Currently, using this, it is only displaying "$playerName1"'s name 10 times. I'm trying to figure out why it's not actually looping through all 10 playerName's. It seems to only be checking $playerName1.
$z = array($playerName1, $playerName2, $playerName3, $playerName4, $playerName5, $playerName6, $playerName7, $playerName8, $playerName9, $playerName10);
$zCounter = 1;
foreach ($z as $allNames) {
while ($allNames != "" && $zCounter < 11) {
echo $allNames . "<br>";
$zCounter++;
}
}
Upvotes: 0
Views: 2075
Reputation: 16828
Unless you want to output each name
10 times, remove the while
loop. You can still check to make sure the name is not empty by using !=''
or empty()
<?php
$z = array($playerName1, $playerName2, $playerName3, $playerName4, $playerName5, $playerName6, $playerName7, $playerName8, $playerName9, $playerName10);
foreach($z as $name){
if(!empty($name)){
echo $name.'<br>';
}
}
Upvotes: 2
Reputation: 639
Your problem is that you are going through the inner while
loop for only the first player name. The outer foreach
loop should be plenty:
foreach ($z as $playerName) {
if ("" !== $playerName) {
echo $playerName . "<br />";
}
}
Upvotes: 4
Reputation: 42756
you need to reset the $zCounter after each while loop
foreach ($z as $allNames) {
while ($allNames != "" && $zCounter < 11) {
echo $allNames . "<br>";
$zCounter++;
}
$zCounter = 0;
}
otherwise after the first while loop finishes, $zCounter will always be 11
Upvotes: 1