dongqing483174 2012-05-18 06:04
浏览 37
已采纳

PHP从查询结果构造多维数组

trying to put together a 3-tiered array of term, class, students for a waitlist clearing page. Here is the foreach I am using to create the array, but something is not right. The resulting array isn't right for looping through and making an html table of term > course >student. My foreach looks like this

    foreach ($active_terms as $term) {
    $waitlists[ $term['term_id'] ] = $term;
        foreach ($active_courses as $course) {
            if ( ($course['term_id'] == $term['term_id']) && ($course['students_waitlisted'] > 0) ) {
            $waitlists[ $term['term_id'] ][ $course['course_id'] ] = $course;                   
                    foreach ($waitlisted_reservations as $reservation) {
                        if ( ($term['term_id'] == $reservation['term_id'])
                                && ($course['course_id'] == $reservation['course_id']) ) {
                            $waitlists[ $term['term_id'] ][ $course['course_id'] ][ $reservation['reservation_id'] ] = $reservation;
                        }   
                    }
            }
        }               
}

and the resulting array is close, but looks like this:

Array
(
[1] => Array
    (
        [term_id] => 1
        [term_title] => Summer Session 1
        [term_begin_date] => June 25 2012
        [3] => Array
            (
                [term_id] => 1
                [course_id] => 3
                [course_title] => Biology 1
                [course_student_limit] => 8
                [students_reserved] => 6
                [students_registered] => 0
                [students_waitlisted] => 3
                [175] => Array
                    (
                        [term_id] => 1
                        [course_id] => 3
                        [reservation_id] => 175
                        [reservation_grade_level] => 12
                        [student_sis_id] => 289055
                    )
                [173] => Array
                    (
                        [term_id] => 1
                        [course_id] => 3
                        [reservation_id] => 173
                        [reservation_grade_level] => 08
                        [student_sis_id] => 111988
                    )
            )
    )
[2] => Array
    (
        [term_id] => 2
        [application_id] => 2
        [term_title] => Summer Session 2
        [term_begin_date] => July 23 2012
        [18] => Array
            ( ....

Could someone please point out how I can make the parent-child nest correctly? Or is this array correct, and I've messed up on how to do the nested foreach to build the table?

  • 写回答

1条回答 默认 最新

  • duan7772 2012-05-18 06:08
    关注

    You have to introduce one more level. For instance, replace:

    $waitlists[ $term['term_id'] ][ $course['course_id'] ] = $course;
    

    With this:

    $waitlists[ $term['term_id'] ]['courses'][ $course['course_id'] ] = $course;
    

    Update

    To keep yourself sane you can also use references in your foreach, like:

    foreach ($active_terms as $term) {
    $waitlists[ $term['term_id'] ] = $term;
        foreach ($active_courses as $course) {
            if ( ($course['term_id'] == $term['term_id']) && ($course['students_waitlisted'] > 0) ) {
            $waitlists[ $term['term_id'] ][ $course['course_id'] ] = $course;  
    

    Can also be written like:

    foreach ($active_terms as $term) {
        $waitlists[ $term['term_id'] ] = $term;
        $current_courses = &$waitlists[ $term['term_id'] ]['courses'];
    
        foreach ($active_courses as $course) {
            if ( ($course['term_id'] == $term['term_id']) && ($course['students_waitlisted'] > 0) ) {
            $current_courses[ $course['course_id'] ] = $course;  
    

    To shorten the variable names. You can probably work out the rest from there :)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog