0

I'm trying to figure out how to place additional text at a specific point inside of a foreach loop, so the output of my php would be:

Alabama Alaska Arizona Arkansas SomeNewText California Colorado Connecticut etc etc etc

continue; and break; don't seem to work in this case. I don't want to stop/quit the foreach loop.

<?php

$items = array("Alabama", "Alaska", "Arizona", "Arkansas", "California", "Colorado", "Connecticut", "Delaware", "Dist of Columbia", "Florida", "Georgia", "Hawaii", "Idaho", "Illinois", "Indiana", "Iowa", "Kansas", "Kentucky", "Louisiana", "Maine", "Maryland", "Massachusetts", "Michigan", "Minnesota", "Mississippi", "Missouri", "Montana", "Nebraska", "Nevada", "New Hampshire", "New Jersey", "New Mexico", "New York", "North Carolina", "North Dakota", "Ohio", "Oklahoma", "Oregon", "Pennsylvania", "Rhode Island", "South Carolina", "South Dakota", "Tennessee", "Texas", "Utah", "Vermont", "Virginia", "Washington", "West Virginia", "Wisconsin", "Wyoming");


$count = 0;

foreach ($items as $item) 
{

if ($count == 3)
{
echo 'SomeNewText';
}

echo $item;
echo "\n";

$count++;

}

?>

Any ideas? Thx.

4
  • your title and your text look like separate questions. Do you want to quit the foreach loop when count reaches 3 ? Commented Feb 11, 2011 at 0:12
  • You definitely don't want a continue there. Aaand yes, you need to close your foreach loop. Other than that, the code should do the job. Commented Feb 11, 2011 at 0:13
  • No, I don't want to quit the foreach loop: I want to inject additional text at some point within the loop. Commented Feb 11, 2011 at 4:55
  • The code does not work, as it outputs "SomeNewText" more than once: at the 4th, 10th, etc, positions: Alabama Alaska Arizona SomeNewText Arkansas California Colorado Connecticut Delaware SomeNewText Dist of Columbia Florida Commented Feb 11, 2011 at 4:57

3 Answers 3

2

That logic looks fine, except you need to close your foreach loop.

$count = 0;
foreach ($items as $item) 
{

 if ($count == 3)
 {
 echo '<td>SomeNewText</td>';
 #continue;
 }

 echo $item;
 echo "\n";

 $count++;
}

Alternatively, you could use a for loop.

Sign up to request clarification or add additional context in comments.

Comments

0

If you mean breaking away from the loop when a condition is met, you can do this:

<?php
foreach ($items as $item) {
  if ($count == 3) {
    break;
  }
}
?>

1 Comment

No, I want to loop thru the whole array, but also I want to be able to add some text somewhere in the middle of the output.
0

Well not to sure what the problem you want sorted, but there is an error in your code you need to close the foreach loop, which should give you the out come you wanted.

echo $item;
echo "\n";

$count++;

} <-- here is missing 

?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.