foreach循环问题与自定义字段

I have this bootstrap slider that doesn't seem to be grabbing the video links properly.

This is the code I have:

        <?php $myCarousel = get_order_field('projectdetails_image'); // use the Custom Group name 
        if( !empty( $myCarousel )) {
                foreach($myCarousel as $carousel){
                $myimg = get('projectdetails_image',1,$carousel);
                    if ( !$myimg ) {
                    $videos = get_order_field('video_vimeo');
                    foreach($videos as $video){
                        if ( get('video_vimeo',TRUE) ) {
                                echo "<div class='item active black'><div id='video-wrap'>";
                                echo get('video_vimeo',1,$video);
                                echo "</div></div>";
                        }
                        else {}
                        }   
                    }
                    if ( $myimg ) {
                        echo "<div class='item".( $counter == 0 ? ' active' : '' )."'>";
                        echo '<img src="';
                        echo $myimg;
                        echo '"/>';
                        echo "</div>";
                        $counter++;

                        $videos = get_order_field('video_vimeo');
                        foreach($videos as $video){
                            if ( get('video_vimeo',TRUE) ) {
                                echo "<div class='item black'><div id='video-wrap'>";
                                echo get('video_vimeo',1,$video);
                                echo "</div></div>";
                            }
                        }
                    }
                }
            }
            ?>

The bottom if statement is giving me problems:

if ( $myimg ) {
                    echo "<div class='item".( $counter == 0 ? ' active' : '' )."'>";
                    echo '<img src="';
                    echo $myimg;
                    echo '"/>';
                    echo "</div>";
                    $counter++;

                    $videos = get_order_field('video_vimeo');
                    foreach($videos as $video){
                        if ( get('video_vimeo',TRUE) ) {
                            echo "<div class='item black'><div id='video-wrap'>";
                            echo get('video_vimeo',1,$video);
                            echo "</div></div>";
                        }
                    }
                }

For some reason its outputting a <div class='item black'> after every <div class='item'> whereas I would like it to output the <div class='item black'> only after all the <div class='item'>'s have been outputted.

I hope that makes sense. If someone could just point me in the right direction that would be great.

Move your for loop outside of the outer for loop, which is causing it to be executed on every iteration.