Skip to content

Instantly share code, notes, and snippets.

@khandar-william
Last active January 9, 2018 11:58
Show Gist options
  • Save khandar-william/0082d62b2a5adf8a082763d3c47860cc to your computer and use it in GitHub Desktop.
Save khandar-william/0082d62b2a5adf8a082763d3c47860cc to your computer and use it in GitHub Desktop.
<?php
$input = <<<TEXT
// Vote text here
TEXT;
preg_match_all('#-\s*(\w[\w\s\(\)]*):([\s,\w]*)#s', $input, $matches);
list($all, $chosen, $voters) = $matches;
$voters = array_map(function ($item) {return array_map('trim', explode(',', $item));}, $voters);
$reversed = [];
$terbanyak = [];
$banyaknya = 0;
foreach ($voters as $key => $who) {
if (count($who) > $banyaknya) {
$terbanyak = [$chosen[$key]];
$banyaknya = count($who);
} else if (count($who) == $banyaknya) {
$terbanyak[] = $chosen[$key];
}
foreach ($who as $person) {
$reversed[$person][] = $chosen[$key];
}
}
$rendered = "Yg sudah memilih sementara:\n";
foreach ($reversed as $person => $choices) {
$string = implode(', ', $choices);
$rendered .= "- {$person}: {$string}\n";
}
$rendered .= "\nMemimpin sementara dengan {$banyaknya} vote: " . implode(', ', $terbanyak) . "\n";
print_r($rendered);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment