<?php
function mobileNumToAlphabet(array $mobileNumA)
{
$mobileAlphabet = array(
'2' => array('a', 'b', 'c'),
'3' => array('d', 'e', 'f'),
'4' => array('g', 'h', 'i'),
'5' => array('j', 'k', 'l'),
'6' => array('m', 'n', 'o'),
'7' => array('p', 'q', 'r', 's'),
'8' => array('t', 'u', 'v'),
'9' => array('w', 'x', 'y', 'z'),
);
$finish = array();
$i = 0;
foreach ($mobileNumA as $key => $value) {
if (empty($mobileAlphabet[$value])) {
unset($mobileNumA[$key]);
continue;
}
if (empty($finish)) {
$finish = $mobileAlphabet[$value];
continue;
}
$i++;
while (!empty($finish)) {
$popStr = array_shift($finish);
if (strlen($popStr) != $i) {
array_unshift($finish, $popStr);
break;
}
foreach ($mobileAlphabet[$value] as $alphabet) {
$str = $popStr . $alphabet;
array_push($finish, $str);
}
}
}
echo json_encode($finish, true);
}
mobileNumToAlphabet(array(2,3));
复制代码
输出
["ad","ae","af","bd","be","bf","cd","ce","cf"]
复制代码