How to get only one result from foreach (PHP)

Encodes an array loop and displays all views for the user. Now everything has changed, and I just need to display one result from the foreach loop. How to do it?

<table class="report_edits_table"> <thead> <tr class="dates_row"> <?php foreach($report['edits'] as $report_edit) : ?> <td colspan="2" report_edit_id="<?php echo $report_edit['id'] ?>"><div class="date_container"> <?php if($sf_user->hasCredential(Attribute::COACHING_EDIT_ACCESS)) : ?> <span class="ui-icon ui-icon-trash">Remove</span> <?php endif?> <?php echo "View " . link_to($report_edit['created'], sprintf('coaching/viewReportEdit?reportedit=%s', $report_edit['id']), array('title' => 'View This Contact')) ?> </div></td> <?php endforeach ?> </tr> </thead> <tbody> <?php foreach($report['edits_titles'] as $index => $title) : ?> <tr class="coach_row"> <?php for ($i=max(0, count($report['edits'])-2); $i<count($report['edits']); $i++) : $report_edit = $report['edits'][$i] ?> <td class="name_column"><?php echo $title ?></td> <td class="value_column"><?php echo $report_edit[$index] ?></td> <?php endfor ?> </tr> <?php endforeach ?> </tbody> 

+4
source share
7 answers

Use the break command for a simple conversion:

 <?php for ... ?> ... stuff here ... <?php break; ?> <?php endfor ... ?> 

A better solution would be to completely remove foreach .

+3
source

It sounds like you want to grab the first element from an array without having to scroll through the rest of them.

PHP provides a set of functions for such situations.

To get the first element in an array, start with the reset() function to position the array pointer at the beginning of the array, then use the current() function to read the element that the pointer is looking at.

So your code will look like this:

 <?php reset($report['edits']); $report_edit = current($report['edits']); ?> 

Now you can work with $report_edits without using the foreach() .

(note that the array pointer actually starts by default in the first entry, so you can skip the reset() call, but it’s best not to do this because it can be changed elsewhere in your code if you are not aware of it)

If after that you want to go to the next record, you can use the next() function. As you can see, if you wanted, it would theoretically be possible to use these functions to write an alternative type of foreach() . There would be no point in using them in this way, but it is possible. But they allow more small-scale control over the array, which is convenient for situations like yours.

Hope this helps.

+4
source

Many ways

  • Direct access to the array element index
  • Refresh any logic that retrieves / generates an array to return only the element of interest
  • Use a for loop that ends after one loop
  • array_filter is your array to get the element of interest
  • A break at the end of your foreach loop, so it ends after the first iteration
  • Conditionally check your index in the foreach loop and only markup of output if the index matches the element of interest
  • And so on

I would recommend just getting the element of interest of the array of interest (number 2 in the list), since this means that your code has less data bouncing from your code (and probably between your PHP block and the database if you populate the array from SQL server )

+3
source

The easiest way?

Put break as the last line of your foreach. It will run once and then exit. (As long as which element you stop, it does not matter).

Secondary method: use array_pop or array_shift on your $report['edits'] or $report['edits_titles'] to get the item, lose the for loop and refer to the item just extracted.

For instance:

 // // current // foreach ($report['edits'] as $report_edit) : /* markup */ endforeach; // // modified version // $report_edit = array_shift($report['edits']); /* markup */ 
+1
source

Use <?php break ?> Before <?php endforeach ?>

+1
source
 example :: <?php foreach ($this->oFuelData AS $aFuelData ) { echo $aFuelData['vehicle']; break; } ?> 
+1
source

You can also use it for reference.

  foreach($array as $element) { if ($element === reset($array)) echo $element; if ($element === end($array)) echo $element; } 
0
source

All Articles