Skip to content

Get Winner / Loser

Original Condorcet method Winner / Loser

php
$winner = $electionWithVotes->getCondorcetWinner();
$loser = $electionWithVotes->getCondorcetLoser();

if ($winner !== null) {
    $winner = $winner->name;
} else {
    $winner = 'There is no winner. Cause of Condorcet paradox.';
}

if ($loser !== null) {
    $loser = 'My loser is ' . (string) $loser ; // Little tip: \CondorcetPHP\Condorcet\Candidate implements the __toString() magic method.
} else {
    $loser = 'There is no loser. Cause of Condorcet paradox.';
}

Get the winner from an advanced method

If there is not a regular Condorcet Winner or Loser, you can process a special winner(s) using an advanced method.

php
use CondorcetPHP\Condorcet\Condorcet;

$electionWithVotes->getWinner(); // With the default object method (Default: Schulze Winning)
$electionWithVotes->getWinner('Copeland'); // Name of a valid method

$electionWithVotes->getLoser(); // With the default object method (Default: Schulze Winning)
$electionWithVotes->getLoser('Kemeny-Young'); // Name of a valid method

Condorcet::getDefaultMethod(); // CondorcetPHP\Condorcet\Algo\Methods\Schulze\SchulzeWinning

When using some advanced Condorcet methods, like Schulze, the getWinner() or getLoser() methods can return one or multiple winners/losers. If there is only one, a Candidate object will be returned, otherwise an array of Candidate objects.

From a Result object

Will return an immutable result object. Winner/Loser will depend on the method. <<< @/code_snippets/get_result_winner_loser.php

Released under the MIT License.