1

I have an array of custom objects and I want to find the objects who have matching property values.

This is my object.php:

class game_list
{
    var $team_name;
    var $field_nr;
    var $time;

    /**
     * @return mixed
     */
    public function getTeamName()
    {
        return $this->team_name;
    }

    /**
     * @param mixed $team_name
     */
    public function setTeamName($team_name)
    {
        $this->team_name = $team_name;
    }

    /**
     * @return mixed
     */
    public function getFieldNr()
    {
        return $this->field_nr;
    }

    /**
     * @param mixed $field_nr
     */
    public function setFieldNr($field_nr)
    {
        $this->field_nr = $field_nr;
    }

    /**
     * @return mixed
     */
    public function getTime()
    {
        return $this->time;
    }

    /**
     * @param mixed $time
     */
    public function setTime($time)
    {
        $this->time = $time;
    }
}

So I have an array with x number of these objects. I want to find objects which have the same $field_nr and $time values. For example:

If two objects on my array, X and Y, both have $field_nr = 1 and $time = "12:00" I want to print out "Match!".

This is what I am doing currently:

//getPlaySchedule returns my array($feedback) of objects
$feedback= getPlaySchedule($cup_name, $cup_year, $division);

for($x=0; $x<count($feedback); $x++){
    $time = $feedback[$x]->getTime();
    $field = $feedback[$x]->getFieldNr();
    $team = $feedback[$x]->getTeamName();

    for($y=0; $y<count($feedback); $y++){
        if($time == $feedback[$y]->getTime() && $field == $feedback[$y]->getFieldNr() && $team != $feedback[$y]->getTeamName()){

            echo 'Match!';
        }
    }
}

My solution, however, prints out "Match!" two times for each match. Is there a better way of finding these matches in my object array?

Marcus

1 Answer 1

1

This is because you not store, what you've checked, and every time loop through the whole array. For example, if you have object A,B,C,D,E,F, first check the A. Let say, A will match D. When you check D, D will match A.

So you need to store it into an array, or insert a pointer, what objects checked already, and when checkin D, skip A, B, C.

A possible solution, if you set a counter, in my example $z, and start the second loop from that. This prevent the script to check again what you've already checked. Code not tested.

$z = 0;
for ($x = 0; $x < count($my_array); $x++) {
    $time = $feedback[$x]->getTime();
    $field = $feedback[$x]->getFieldNr();
    $team = $feedback[$x]->getTeamName();
    for ($y = $z; $y < count($my_array); $y++) {
        if ($time == $feedback[$y]->getTime() && $field == $feedback[$y]->getFieldNr() && $team != $feedback[$y]->getTeamName()) {
            echo 'Match!';
        }
    }
    $z++;
}
Sign up to request clarification or add additional context in comments.

Comments

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.