Last active
January 9, 2018 11:58
-
-
Save khandar-william/0082d62b2a5adf8a082763d3c47860cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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