Php 查找While循环中第一个出现的字符串

Php 查找While循环中第一个出现的字符串,php,while-loop,Php,While Loop,我需要尝试在while循环中查找字符串的第一次出现。如果可以避免的话,我宁愿不使用Jquery来查找每个元素的第一个 while ( $teacher_assignment_query->have_posts() ) { $teacher_assignment_query->the_post(); $assignment_fields = get_post_custom($post->ID); //print '$assignment_fields['t

我需要尝试在while循环中查找字符串的第一次出现。如果可以避免的话,我宁愿不使用Jquery来查找每个元素的第一个

while ( $teacher_assignment_query->have_posts() ) {
    $teacher_assignment_query->the_post();
    $assignment_fields = get_post_custom($post->ID);
    //print '$assignment_fields['title'][0] and stuff here
});
这会打印出这样的作业列表

<li class="assignments fourthgrade"><a href="#">Do worksheet 2-1</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-2</a></li>
<li class="assignments fourthgrade"><a href="#">Do worksheet 1-1</a></li>
<li class="assignments fifthgrade"><a href="#">Volunteer somewhere</a></li>
<li class="assignments fifthgrade"><a href="#">Finish science project</a></li>
$last_title='';
while($teacher\u assignment\u query->have\u posts()){
$teacher_assignment_query->the_post();
$assignment\u fields=get\u post\u custom($post->ID);
如果($assignment_fields['grade']!=$last_title){
回显“
  • ”.$assignment_字段['grade']”。
  • ; $last_title=$assignment_字段['grade']; } //在此处打印“$assignment_字段['title'][0]和其他内容 });
    $last\u title='';
    while($teacher\u assignment\u query->have\u posts()){
    $teacher_assignment_query->the_post();
    $assignment\u fields=get\u post\u custom($post->ID);
    如果($assignment_fields['grade']!=$last_title){
    回显“
  • ”.$assignment_字段['grade']”。
  • ; $last_title=$assignment_字段['grade']; } //在此处打印“$assignment_字段['title'][0]和其他内容 });
    您可以使用临时变量将当前结果与以前的结果进行比较,并在不同时更改标题。
    例如(元代码):

    $previous_grade=”“;
    while(条件){
    //一些代码来获取你的数据
    [...]
    //将当前等级与上一等级进行比较
    $current_grade=$assignment_字段['grade'];
    如果($current_grade!=$previous_grade){
    打印“
  • $current\u grade
  • ”; } //继续列清单 打印$assignment_字段['title'][0]和其他内容; //更新临时变量 $previous_grade=$current_grade; }
    您可以使用临时变量将当前结果与以前的结果进行比较,并在不同时更改标题。
    例如(元代码):

    $previous_grade=”“;
    while(条件){
    //一些代码来获取你的数据
    [...]
    //将当前等级与上一等级进行比较
    $current_grade=$assignment_字段['grade'];
    如果($current_grade!=$previous_grade){
    打印“
  • $current\u grade
  • ”; } //继续列清单 打印$assignment_字段['title'][0]和其他内容; //更新临时变量 $previous_grade=$current_grade; }
    <li class="heading">Fourth Grade</li> //new heading
    <li class="assignments fourthgrade"><a href="#">Do worksheet 2-1</a></li>
    <li class="assignments fourthgrade"><a href="#">Do worksheet 1-2</a></li>
    <li class="assignments fourthgrade"><a href="#">Do worksheet 1-1</a></li>
    
    <li class="heading">Fifth Grade</li> //new heading
    <li class="assignments fifthgrade"><a href="#">Volunteer somewhere</a></li>
    <li class="assignments fifthgrade"><a href="#">Finish science project</a></li>
    
    $last_title = '';
    while ( $teacher_assignment_query->have_posts() ) {
        $teacher_assignment_query->the_post();
        $assignment_fields = get_post_custom($post->ID);
        if($assignment_fields['grade']!=$last_title){
            echo '<li class="heading">'.$assignment_fields['grade'].'</li>';
            $last_title = $assignment_fields['grade'];
        }
        //print '$assignment_fields['title'][0] and stuff here
    });
    
    $previous_grade="";
    while(conditions) {
        // some code to get your data
        [...]
        // Compare the current grade with the previous one
        $current_grade=$assignment_fields['grade'];
        if($current_grade!=$previous_grade) {
            print "<li class=\"heading\">$current_grade</li>";
        }
        // Go ahead with the list
        print $assignment_fields['title'][0] and other stuffs;
        // Update the temporary variable
        $previous_grade=$current_grade;
    }