Count distinct (not null) values in multi array

I am sure this will be something simple but I need to check if certain classes have been selected on certain days to therefore count how many days classes have been entered on (hope this makes sense) - for example say there are class numbers 1 and 2 on a Saturday (day_id=6) and class numbers 3 & 4 on Sunday (day_id=7) I need to find out if the person has selected classes on Saturday or Sunday or both. I already have a lot of functionality checking for number of classes entered ( including per day) but I can’t seem to find how to convert this into counting how many days.

I have run some tests and selected one class on Saturday and 2 on Sunday and when I print out one piece of code I am getting somewhere:

print_r($days_form[$classID])

This gives me the result 677 which is obviously show one class selected for day_id 6 and 2 classes for day_id 7 but I can’t get any further and am thinking there must be some way of using the printed array to check if classes have been selected for each day_id.

Thank you in advance for any help (I am sure this is not explained very well but hope someone can understand and help me).

If you’re looking for help with the code, you should make the structure of your array obvious and clear to us.

Otherwise, one(of many) ways could be:
Make a new array, appending a day_id for each class.
Remove duplicates.
Count.

Sorry for the delay (I thought I had posted this) but the form code that is posted is:

<input type="hidden" name="day_id_normal[<?php echo $graderow['class_number'];?>]" value="<?php echo $dayrow['day_id'];?>" />
                            <input type="hidden" name="day_id[<?php echo $graderow['class_number'];?>]" value="<?php echo $dayrow['day_id'];?>" />
                            <input type="checkbox" name="class_number[]" id="gradeclass" value="<?php echo $graderow['class_number']?>" <?php if (in_array($graderow['class_number'], $_SESSION['classes'])) {  ?> checked="checked" <?php } ?> />

The relevant php on the next page that creates/uses the arrays is:


//counts number of classes per day
$normal_classes_form = (isset ($_POST['class_number'])) ? $_POST['class_number'] : array();
$normal_days_form= (isset ($_POST['day_id_normal'])) ? $_POST['day_id_normal'] : array();
$normal_day=array_values(array_unique($normal_days_form));
$count_normal_classes=0;
foreach ($normal_day as $normaldayID) {
    foreach ($normal_classes_form as $normalclassID) {
        if ($normal_days_form[$normalclassID]!=$normaldayID) continue;
        $count_normal_classes++;
    }
}
//End

$classes_form1 = (isset ($_POST['class_number'])) ? $_POST['class_number'] : array();
$days_form = (isset ($_POST['day_id'])) ? $_POST['day_id'] : array();

$day=array_values(array_unique($days_form));
$classes_error=0;


foreach ($day as $dayID) {

    $count_classes_per_day=0;
    $count_exclude=0;
    $sql=pg_query("SELECT shows_day_class_limit.max_classes, shows_day_class_limit.max_classes_incl, shows_day_class_limit.classes_text, days.day
                FROM shows_day_class_limit
                INNER JOIN days ON days.day_id=shows_day_class_limit.day_id
                WHERE shows_day_class_limit.day_id={$dayID} AND shows_day_class_limit.show_id='".$_SESSION['show_id']."'");
    $days=pg_fetch_array($sql);
    $max_classes=$days['max_classes'];
    $max_classes_incl=$days['max_classes_incl'];
    $classes_text=$days['classes_text'];
    $day_name=$days['day'];
    
    foreach ($classes_form1 as $classID) {
    // If this class isn't being entered on the current day, skip and move on to the next class
    if ($days_form[$classID]!=$dayID) continue;
    
    if (!empty($max_classes_incl) && $max_classes_incl>0) { 
        $sql=pg_query("SELECT limit_excl FROM shows_classes WHERE class_number={$classID} AND show_id='".$_SESSION['show_id']."'");
        $query=pg_fetch_array($sql);
        $exclude=$query['limit_excl'];
        if ($exclude=='t') {
        $count_exclude++;
        }
    }
    $count_classes_per_day++;
        if ($count_classes_per_day-$count_exclude>$max_classes) {
        $classes_error++;
            $day_class_info = '<p class="highlight">'.$day_name.'</p><p>Classes Entered: '.$count_classes_per_day.'<br />';
            $day_class_info .= '<strong>'.$classes_text.'</strong></p>';
        }
    }

// Loop through each handler and their classes
    foreach ($_POST['class_number'] as $handler_id=>$classes) {
    $total_champ_classes=0;
    $total_classes=0;

        //$total_classes=count($classes);
        $classes_serial=serialize($classes);
        
        $check_champ=unserialize($classes_serial);
        foreach ($check_champ as $class_no) {
            $sqlchamp=pg_query("SELECT classes.class_type_id FROM classes INNER JOIN shows_classes ON shows_classes.class_id=classes.class_id WHERE shows_classes.class_number='".$class_no."' AND shows_classes.show_id='".$_SESSION['show_id']."'");
            $querychamp=pg_fetch_array($sqlchamp);
            if ($querychamp['class_type_id']==8) {
            $total_champ_classes++;
            }
            else { $total_classes++; }
        }
        //if ($total_champ_classes>0) { $total_classes=$total_classes-$total_champ_classes; }
        
        if (!empty($_SESSION['free_dog_id']) && $_SESSION['free_dog_id']==$_SESSION['dog_id']) {
        $free_entries='t';
        }
        else {
        $free_entries='f';
        }
        
        $sql=pg_query("SELECT entry_id, total_classes, total_champ_classes FROM shows_class_entries WHERE show_id='".$_SESSION['show_id']."'
                        AND handler_id='".$handler_id."' AND dog_id='".$_SESSION['dog_id']."'");
        $query=pg_fetch_array($sql);
        $curr_entry_id=$query['entry_id'];
        $curr_total_classes=$query['total_classes'];
        $curr_total_champ_classes=$query['total_champ_classes'];
        
        if (!empty($curr_entry_id)) {
        
         pg_query("UPDATE shows_class_entries set 
            classes='" . $classes_serial . "',
            total_classes='" . $total_classes . "',
            temp_ref='" . pg_escape_string($_SESSION['tempref']) . "',
            updated='NOW()',
            curr_dog_grade='" . $_SESSION['grade_id'] . "',
            free_entries='" . $free_entries . "',
            club='" . $club . "',
            total_champ_classes='".$total_champ_classes."'
            WHERE entry_id='".$curr_entry_id."'");
        
        }
        else {
        if (!empty($_SESSION['withold_address'])) {
            if ($_SESSION['withold_address']==0) { $show_address='t'; }
            else { $show_address='f'; }
        }
        else { $show_address='t'; }
        
            pg_query("INSERT INTO shows_class_entries (customer_id, show_id, dog_id, handler_id, classes, total_classes, temp_ref, added, added_by_owner, entry_form, curr_dog_grade, free_entries, club, total_champ_classes, show_address) values (
            '" . pg_escape_string($_SESSION['customer_id']) . "',
            '" . pg_escape_string($_SESSION['show_id']) . "',
            '" . pg_escape_string($_SESSION['dog_id']) . "',
            '" . $handler_id . "',
            '" . $classes_serial . "',
            '" . $total_classes . "',
            '" . pg_escape_string($_SESSION['tempref']) . "',
            'NOW()',
            '" . pg_escape_string($added_by_owner) . "',
            '" . $entry_form . "',
            '" . $_SESSION['grade_id'] . "',
            '" . $free_entries . "',
            '" . $club . "',
            '" . $total_champ_classes . "',
            '" . $show_address . "')");
        
        }

I hope this now provides enough information.