每个数据的php要以逗号分隔的列表显示,没有空格

时间:2019-05-26 00:16:06

标签: php wordpress

当前,我正在显示用户ID,并用逗号定界符在一行中显示,问题是逗号之间有多余的空格

这样显示 1、6、10

应该是这样 1,6,10

  <div class="students">
<?php foreach ( $students as $student ) {
				  
          $process = '';
          $result = 0;
          $student = learn_press_get_user( $student->ID );
          $data = $student->get_course_data($course->get_id());
          $status = $student->get_course_status($course->get_id());
          $result = $data->get_results();
   ?>

		<?php if ( $status ) {
		$process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
		} ?>
		
       <?php echo $student->get_id(); ?>,
        <?php } ?>
        </div>

enter image description here

3 个答案:

答案 0 :(得分:1)

<div class="students">
    <?php 

        foreach ( $students as $student ) {
            $process = '';
            $result = 0;
            $student = learn_press_get_user( $student->ID );
            $data = $student->get_course_data($course->get_id());
            $status = $student->get_course_status($course->get_id());
            $result = $data->get_results();

            if ( $status ) {
            $process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
            } 

            echo trim($student->get_id()) . ',';
        }
    ?>
</div>

答案 1 :(得分:1)

使用implode

的另一种解决方案
<div class="students">
  <?php 
  $student_ids = array();
  foreach ( $students as $student ) {
         $process = '';
         $result = 0;
         $student = learn_press_get_user( $student->ID );
         $data = $student->get_course_data($course->get_id());
         $status = $student->get_course_status($course->get_id());
         $result = $data->get_results();
         if ( $status ) {
           $process = ( $status == 'finished' ) ? 'finished' : 'in-progress';
         } 
         $student_ids[] =  $student->get_id(); // store all student id in array
    } 
    echo implode(',', $student_ids);
  ?>
</div>

答案 2 :(得分:0)

最后div结束时的php标记,在其前面有一个空格。

 <?php } ?>

尝试在一个PHP代码段中编写循环。