使用带有else语句的两个foreach循环

I have two arrays where the the arrays of locations of Images . When I use the first for each loop I get the images from location. The sript is not working if I use an else statment.
I am using the below arrays as $image_name.

Any ideas.

$cam_list1 = array(1,2,3,4,5,6,7,8);
$cam_list2 = array(9,10,11,12,13,14,15,16);


foreach ($cam_list1 as $cam) {
    $timestamp = strtotime($displayEntryDatetime);
    $cam_delta = 14;
    $timestamp = $timestamp - $cam_delta;
    for ($i = 0; $i < $cam_delta+2; $i++) {
        $cdate = date("d_m_Y* H_i_s", $timestamp);
        $image_name = "/xampp/htdocs" . $damage_topdir1. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
        foreach (glob($image_name) as $filename) {
            if (file_exists($filename)) {
                $fs_image = str_replace("/xampp/htdocs", "", $filename);
                print "<h3>Camera $cam</h3>";          
                print "<a href=\"$fs_image\" target=\"_new\"><img src=\"$fs_image\" height=240 width=320 /></a>
";
            }
        }
        $timestamp++;
    }
}
else 

    foreach ($cam_list2 as $cam) 
    {
        $timestamp = strtotime($displayEntryDatetime);
        $cam_delta = 14;
        $timestamp = $timestamp - $cam_delta;
        for ($i = 0; $i < $cam_delta+2; $i++) {
            $cdate = date("d_m_Y* H_i_s", $timestamp);
            $image_name = "/xampp/htdocs" . $damage_topdir2. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
            foreach (glob($image_name) as $filename) {
                if (file_exists($filename)) {
                    $fs_image = str_replace("/xampp/htdocs", "", $filename);
                    print "<h3>Camera $cam</h3>";          
                    print "<a href=\"$fs_image\" target=\"_new\"><img src=\"$fs_image\" height=240 width=320 /></a>
";
                }
            }
            $timestamp++;
        }
    }

You can use ELSE statement ONLY if you use IF statement before. A PHP Fatal Error will be displayed in your case.

I don't clearly understand that you want to do but if you want to check if you have images in your lists, you can use a function to execute foreach process in cam_list.

function getImages($list) {
    $inList = false;

    foreach ($list as $cam) {
        $timestamp = strtotime($displayEntryDatetime);
        $cam_delta = 14;
        $timestamp = $timestamp - $cam_delta;
        for ($i = 0; $i < $cam_delta+2; $i++) {
            $cdate = date("d_m_Y* H_i_s", $timestamp);
            $image_name = "/xampp/htdocs" . $damage_topdir1. $cam . "/Cam*" . $cam . "*-" .$cdate . "*";
            foreach (glob($image_name) as $filename) {
                if (file_exists($filename)) {
                    $inList = true;
                    $fs_image = str_replace("/xampp/htdocs", "", $filename);
                    print "<h3>Camera $cam</h3>";          
                    print "<a href=\"$fs_image\" target=\"_new\"><img src=\"$fs_image\" height=240 width=320 /></a>
";
                }
            }
            $timestamp++;
        }
    }

    return ($inList) ? true : false;
}

With an if/else you can after get images in the cam_list1 or in the cam_list2 otherwise.

$cam_list1 = array(1,2,3,4,5,6,7,8);
$cam_list2 = array(9,10,11,12,13,14,15,16);

if (!getImages($cam_list1)) {
    getImages($cam_list2);
}