Course
という名前のオブジェクト(適切な名前)Course
が配列内にあるかどうかを調べるために、in_array()
を使用しています。PHP in_arrayでオブジェクトの比較を行う方法を変更する
1つの問題は、オブジェクト全体の比較ではなく、オブジェクトの比較を行うためにCourse
オブジェクト内の特定のプロパティを使用することです。
具体的には、$course->getShortName()
を使用して比較を行います。どうして?私のCourse
オブジェクト内の他のすべてのプライベート変数は同じであることができるshort_name
プロパティを除いて異なる可能性があります。そのため、オブジェクトプロパティを実行するために使用したいのです。私Course
クラス
public function getShortName(){
return $this->short_name;
}
tl;dr
から
public function overlap($courses, $course_temp) {
for ($i = 0; $i < count($courses); $i = $i + 1) {
if ($this->overlapCourses($courses[$i], $course_temp)) {
// Push the class that is conflicted to the conflictedClass
// array
// TODO: Figure out why it's being added to the list
if(!in_array($courses[$i], $this->conflictClasses)) {
array_push($this->conflictClasses, $courses[$i]);
}
// Push the class that is conflicted with to the
// conflictedClass array
// TODO: Figure out why it's being added to the list
if(!in_array($course_temp, $this->conflictClasses)) {
array_push($this->conflictClasses, $course_temp);
}
return false;
}
}
return false;
}
ゲッター:比較を行い
メソッドの代わりに、比較対象のオブジェクト
in_arrayの動作を変更することはできませんが、おそらくこれは参考になります:http://php.net/manual/de/function.array-uintersect.php – colburton