function printWord(string $digits, array $letters) {
$chars = str_split($digits);
$flipped = array_flip($letters);
$result = '';
foreach ($chars as $char) {
$result .= $flipped[$char];
}
echo $result;
}
$testLetters = [
'ч' => 0,'л' => 9, 'в' => 1,'к' => 8,'е' => 9,'о' => 2,
];
printWord('0992158', $testLetters);
$testLetters
.Вопрос: если в массиве ключи имеют одинаковые значение,то как вывести рандомный ключ из этих значений? function printWord(string $digits, array $letters) {
$chars = str_split($digits);
$result = '';
foreach ($chars as $char) {
$current = array_keys($letters, $char);
$countCurrent = count($current);
$result .= ($countCurrent > 1) ? $current[rand(0, ($countCurrent - 1))] : $current[0];
}
return $result;
}
$testLetters = [
'ч' => 0,'л' => 9, 'в' => 1,'к' => 8,'е' => 9,'о' => 2,
];
echo printWord('0992158', $testLetters);