$source = ['a' => 1, 'b' => 2, 'c' => 1];
$result = [];
foreach ($source as $key => $value) {
if(array_key_exists($result, $value)) {
if (is_array($result[$value])) {
$result[$value][] = $key;
} else {
$result[$value] = [$result[$value], $key];
}
} else {
$result[$value] = $key;
}
}