0

大家好,我有点卡住了,需要一些帮助。我有一个返回以下内容的查询

Array
(
[0] => stdClass Object
    (
        [id] => 1
        [username] => Joe Blogs
        [user_number] => JB222
        [email] => jb@kemail.com
        [job_title] => Teacher
        [contractor] => Direct Hire
        [campus] => Male Humanities
        [role] => Teacher
        [section_number] => 11111 | 213312 | 232121 | 432231
        [stid] => 89,91,95,94
        [classroom] => 2621,1329,1428,3123
        [subject] => English | Physics | English | Maths
    )

)

我需要能够将其分解,以便我可以循环遍历 stid、部分、教室和主题,作为嵌套循环的一部分。我想将部分和教室一起加入,并将 stid 分开,以便我可以在链接中使用它。我也想把班级分开。

                                     -----------------------------------------------
                                     |    sections    |  classroom   |   Teacher    |
                                     -----------------------------------------------
this would be a link with stid 89 -> | English 11111  |    2621      |  Joe Blogs   |
this would be a link with stid 91 -> | Physics 213312 |    1329      |              |
                                     | English 232121 |    1428      |              |
                                     | Maths   432231 |    3123      |              |
                                     -----------------------------------------------

我尝试了不同的东西,但无法让它们工作。以下是我尝试过的,但我可以弄清楚如何将每个 stid 作为链接中的值。如果班级为空,我还尝试使用 if 语句说 N/A 但这也不起作用。

旧代码

<ul>

@if ( empty($teacher->subject) )
N/A 
@else   
 <?php 
     $sections = explode(' | ',$teacher->section_number );
 $subjects = explode(' | ',$teacher->subject);  
 $data = array_combine($sections,$subjects); ?>

      @foreach ( $data as $section=>$class)     
      <li>{{ $section . " - " .$class }}</li>
      @endforeach

@endif

</ul>
</td>
<td> 
<ul>

 <?php $classrooms = explode(',',$teacher->classroom); ?>
@foreach ( $classrooms as $room)    
    <li>{{ $room }}</li>
@endforeach

</ul>

任何帮助,将不胜感激。特别是对我哪里出错的一些解释。谢谢

4

1 回答 1

0
/* NEEDED FOR TESTING 
$teacher = new StdClass;
$teacher->id = 1;
$teacher->username = 'Joe Blogs';
$teacher->user_number = 'JB222';
$teacher->email = 'jb@jbkemail.com';
$teacher->job_title = 'Teacher';
$teacher->contractor = 'Direct Hire';
$teacher->campus = 'Male Humanities';
$teacher->role = 'Teacher';
$teacher->section_number = '11111 | 213312 | 232121 | 432231';
$teacher->stid = '89,91,95,94';
$teacher->classroom = '2621,1329,1428,3123';
$teacher->subject = 'English | Physics | English | Maths';
*/

$stid = explode(",",$teacher->stid);
$classroom = explode(",",$teacher->classroom);
$subject = explode(" | ",$teacher->subject);

foreach($stid as $k=>$s){
    echo $subject[$k].' '.$classroom[$k].' '.$s.'<br>';
}
于 2012-11-27T20:17:23.413 回答