简体   繁体   中英

php foreach loop creating doubles from nested array how to fix

I am trying to display values from a large array into a specific html table layout for an email template. I can get close with the format I require however I can't get the foreach loop right where it doesn't create doubles.

This is the current output in html 在此处输入图片说明

This is the foreach loop that I'm currently using that outputs doubles:

        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>";
    };

And this is the array I currently draw data from. I can alter the way the array is arranged, just getting stuck as I'm not as familiar with nested arrays and how to loop through them all without causing doubles:

"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
]

]

As your looping over the title, you have the key which you can then match up with the other items. You dont need multiple loops.

<?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

}

Also, them echos are horrid, instead either break out of PHP or use a template.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM