0

In the code below, I create an object instance $T, give its properties values, and add it to my array of objects called $teacherObjArray. However, where I'm having trouble is if the object instance already exists (this is checked by the teacherExists() function)... how does does the program know which $T object I am accessing? In other words, How can I program it to where it UPDATES the CORRECT within the array of objects? My definition of correct is where $teacherName is the same as object property LName. I appreciate the help :)

function teacherExists($teacherObjArray, $teacherName) {
    foreach ($teacherObjArray as $Teacher) {
        if ($Teacher->LName == $teacherName) return true; //can i return index here? 
    }
    return false;
}
while(sizeOf($r) > 0) 
{
     if (!teacherExists($teacherObjArray, $teacherName)) {
        $T = new Teacher($teachernName, $teacherMaxLoad, $classPeriod); 
        $T->LName = $teacherName;
        $T->MaxLoad = $teacherMaxLoad; 
        $T->addPeriod($classPeriod);  
        array_push($teacherObjArray, $T); 

        //create course object

        $C = new Course($courseNumber, $courseName, $teacherName, $classPeriod); 
        $C->CourseNumber=$courseNumber;
        $C->CourseName=$courseName; 
        $C->CourseTeacher=$teacherName;
        $C->CoursePeriod=$classPeriod; 

        maintenance($r, $teacherName, $classPeriod, $cn); 
    }
        //updates already existing object in array of objects
    elseif (teacherExists($teacherObjArray, $teacherName)) 
    {
        //get teacher object where its LName property is equal to $teacherName 

        $T->addPeriod($classPeriod);
        maintenance($r, $teacherName, $classPeriod, $cn); 
    }
}
2
  • What does $r represent? Commented Apr 9, 2020 at 6:34
  • $r is the array I have created using mysqli_fetch_array, it is essentially taking a query and representing it in an array form. Commented Apr 9, 2020 at 6:43

1 Answer 1

1

You can rewrite teacherExists to return the index of the found value, or false when not found:

function teacherExists($teacherObjArray, $teacherName) {
    foreach ($teacherObjArray as $key => $Teacher) {
        if ($Teacher->LName == $teacherName) return $key; 
    }
    return false;
}

and then you can rewrite your if clause to check that the return value is false:

if (($key = teacherExists($teacherObjArray, $teacherName)) === false) {

and your elseif clause simply becomes:

else
{
    //get teacher object where its LName property is equal to $teacherName 
    $T = $teacherObjArray[$key];
    $T->addPeriod($classPeriod);
    maintenance($r, $teacherName, $classPeriod, $cn); 
}

Note that since you call

maintenance($r, $teacherName, $classPeriod, $cn); 

at the end of both the if and else clauses, you could move it after the if block.

Sign up to request clarification or add additional context in comments.

1 Comment

Another thing to add is that the maintenance function can be called after the if/else statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.