对象 foreach 循环只迭代偶数值

use*_*144 2 php

基本上我有一组对象值我想在 2 个单独的 html 列表中呈现

我认为最简单的方法是只在一个列表中显示偶数,而只在另一个列表中显示奇数

这是显示单个列表的当前代码

   <ul>
    <?php foreach ($values as $value) : ?>
    <li><?php echo $value->value; ?></li>
    <?php endforeach; ?>
  </ul>
Run Code Online (Sandbox Code Playgroud)

Ste*_*tei 5

尝试这个:

<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; ?>
Run Code Online (Sandbox Code Playgroud)