对象foreach循环只迭代偶数值

Basically I have a set of object values I want render in 2 separate html lists

I figure the simplest way to do this is by displaying evens only in one list, and odd only in the other

Here is the current code for displaying a single list

   <ul>
    <?php foreach ($values as $value) : ?>
    <li><?php echo $value->value; ?></li>
    <?php endforeach; ?>
  </ul>

Try this:

<ul>

<?php 
/* read the index key */
foreach ($values as $key => $value) : 

 /* skip the current element if it doesn't have an even index */
 if($key % 2 == 1) continue; 

?>
<li><?php echo $value->value; ?></li>
<?php endforeach; ?>

You didn't specify if the array has integer index. So I use a separate index pivot. This will do.

$v=array();
$index = 1;
foreach ($values as $value){
   $v[($index++)%2][]=$value->value;
}
list ($evens, $odds) = $v;

echo "<ul><li>".implode("</li><li>", $odds)."</li></ul>"; // show list of odds
echo "<ul><li>".implode("</li><li>", $evens)."</li></ul>"; // shows list of even