0

I'm trying to rewrite two foreach loops as for loops so that I am able to stop them at 3. Here is the first and simple loop's original:

<?php foreach ($marketing[0]['values'] as $company) { ?>
            <tr>
              <td><?php echo $company['label']; ?></td>
              <td><?php echo $company['value']; ?></td>
            </tr>
          <?php } ?>

Here is my attempted re-write:

 <?php 
        for($i = 0; $i < 4; ++$i) { 
        $company = $marketing[0][$i]['values']; 
      ?>
        <tr>
          <td><?php echo $company['label']; ?></td>
          <td><?php echo $company['value']; ?></td>
        </tr>            
      <?php } ?>

Here is the second and slightly more complicated foreach loop that I haven't attempted yet.

<?php foreach ($sales as $sale) { ?>
          <tr>
            <td><?php echo $sale['key']; ?></td>
            <td>
              <?php 
                foreach ($sale['values'] as $values) {
                  if ($values['x'] == $currentTeam) {
                    echo $values['y'];
                  }
                }                 
              ?>
            </td>
          </tr>
          <?php } ?>
6
  • $company = $marketing[0]['values'][$i]; Commented Aug 6, 2014 at 12:12
  • 1
    Btw, what is the point for such a rewrite? Why cannot you just stop foreach with break? Commented Aug 6, 2014 at 12:12
  • You can also stop foreach loops at 3 as well. Commented Aug 6, 2014 at 12:13
  • I'm simply open to the best possibly implementation. The examples of break I found didn't match the kind of code I was using so not sure how to implemented. Commented Aug 6, 2014 at 12:14
  • 1
    @dvoutt: "I'm simply open to the best possibly implementation" --- your question lacks the explanation of the real issue to help you. Now this question is a great example of an XY-problem Commented Aug 6, 2014 at 12:15

2 Answers 2

2

You can always use break. http://php.net/manual/en/control-structures.break.php

<?php $counter = 0; ?>
<?php foreach ($marketing[0]['values'] as $company) { ?>
    <?php if ($counter == $maxLimit) break; ?>
    <tr>
        <td><?php echo $company['label']; ?></td>
        <td><?php echo $company['value']; ?></td>
    </tr>
    <?php $counter++; ?>
<?php } ?>
Sign up to request clarification or add additional context in comments.

2 Comments

To go further one may combine them if (++$counter <= $maxLimit) break;
For some reason the editor is stripping out the closing PHP tags on lines 1 and 8. Make sure to include those.
1

You were almost there already

<?php 
  $maxLimit = 3; //you could get maxlimit from elsewhere rather than hardcoding it

  //use count() to get the length of an array in PHP
  for($i = 0; $i < count($marketing[0]); ++$i) 
  { 
    $company = $marketing[0][$i]['values']; 
    if($i > $maxLimit) break;
  ?>
    <tr>
      <td><?php echo $company['label']; ?></td>
      <td><?php echo $company['value']; ?></td>
    </tr>            
  <?php } ?>

As an alternative, you could introduce a counter field on the side

      <?php 
          $salesCounter = 0;
          $maxSalesCounter = 3;
          foreach ($sales as $sale) 
          { 
      ?>
      <tr>
        <td><?php echo $sale['key']; ?></td>
        <td>
          <?php 
            foreach ($sale['values'] as $values) 
            {
              if($salesCounter > maxSalesCounter) break;
              if ($values['x'] == $currentTeam) 
              {
                echo $values['y'];
                $salesCounter++;
              }

            }                 
          ?>
        </td>
      </tr>
      <?php } ?>

1 Comment

Or use the minimum of 3 and count.

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.