php foreach循环从嵌套数组创建双打如何修复

时间:2018-03-12 04:46:48

标签: php arrays foreach nested

我正在尝试将大型数组中的值显示为电子邮件模板的特定html表格布局。我可以接近我需要的格式,但是我无法在不创建双精度的地方获得foreach循环。

这是html中的当前输出 enter image description here

这是我正在使用的foreach循环输出双精度:

        foreach($inputs['course_details']['course_name'] as $courses){ 
        echo "<tr>";

    echo "<tr><td<strong>" . $courses . "</strong></td></tr>";
    foreach($inputs['course_details']['study_mode'] as $study_modes){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Study Mode</td>";
                echo "<td style='font-size:12px;'>";
                echo $study_modes;
                echo "</td>";
                echo "</tr>";
            }
    foreach($inputs['course_details']['commencement'] as $commencement){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Commencement</td>";
                echo "<td style='font-size:12px;'>";
                echo $commencement;
                echo "</td>";
                echo "</tr>";
            }
    foreach($inputs['course_details']['allowable_time'] as $allowable_time){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Allowable Time</td>";
                echo "<td style='font-size:12px;'>";
                echo $allowable_time;
                echo "</td>";
                echo "</tr>";
            }
    foreach($inputs['course_details']['delivery_campus'] as $delivery_campus){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Delivery Campus</td>";
                echo "<td style='font-size:12px;'>";
                echo $delivery_campus;
                echo "</td>";
                echo "</tr>";
            }
    foreach($inputs['course_details']['delivery_intake'] as $delivery_intake){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Delivery Intake</td>";
                echo "<td style='font-size:12px;'>";
                echo $delivery_intake;
                echo "</td>";
                echo "</tr>";
            }
    foreach($inputs['course_details']['F2F_duration'] as $F2F_duration){
                echo "<tr>";
                echo "<td style='font-size:12px;'>Face to Face Duration</td>";
                echo "<td style='font-size:12px;'>";
                echo $F2F_duration;
                echo "</td>";
                echo "</tr>";
            }       
    echo "<td style='font-size:12px;'>Days and Times</td>";
    echo "<td style='font-size:12px;'>";
            foreach($inputs['course_details']['days_times'] as $days){

            echo $days[0];          

            }
    echo "</td>";
    echo "</tr>";
    };

这是我目前从中提取数据的数组。我可以改变数组的排列方式,因为我不熟悉嵌套数组以及如何在不引起双打的情况下遍历它们而陷入困境:

"course_details" => array:10 [▼
"days_times" => array:9 [▶]
"order_id" => array:2 [▼
  0 => 546
  1 => 545
]
"course_id" => array:2 [▼
  0 => 16
  1 => 15
]
"course_name" => array:2 [▼
  0 => "Course 2"
  1 => "Course 1"
]
"study_mode" => array:2 [▼
  0 => "Part Time On-Campus"
  1 => "Full Time On-Campus"
]
"commencement" => array:2 [▼
  0 => null
  1 => null
]
"allowable_time" => array:2 [▼
  0 => "14"
  1 => "7"
]
"delivery_intake" => array:2 [▼
  0 => null
  1 => "2018-03-22 00:00:00"
]
"delivery_campus" => array:2 [▼
  0 => "Campus 2"
  1 => "Campus 1"
]
"F2F_duration" => array:2 [▼
  0 => 14
  1 => 7
]

1 个答案:

答案 0 :(得分:2)

当你在标题上循环时,你有一把钥匙,然后你可以与其他项目匹配。你不需要多个循环。

<?php

foreach ($inputs['course_details']['course_name'] as $course_key => $courses) { 
  echo "<tr>";
  echo "<tr><td<strong>" . $courses . "</strong></td></tr>";
  echo "<tr>";
  echo "<td style='font-size:12px;'>Study Mode</td>";
  echo "<td style='font-size:12px;'>";
  echo $inputs['course_details']['study_mode'][$course_key];
  echo "</td>";
  echo "</tr>";

  //... and so on

}

此外,他们的回声是可怕的,而不是打破PHP或使用模板。