1. <?php
    2. class Solution {
    3. public $res = [];
    4. public $str = "";
    5. public $array = [
    6. '2'=> ['a', 'b', 'c'],
    7. '3'=> ['d', 'e', 'f'],
    8. '4'=> ['g', 'h', 'i'],
    9. '5'=> ['j', 'k', 'l'],
    10. '6'=> ['m', 'n', 'o'],
    11. '7'=> ['p', 'q', 'r', 's'],
    12. '8'=> ['t', 'u', 'v'],
    13. '9'=> ['w', 'x', 'y', 'z'],
    14. ];
    15. public function letterCombinations($digits) {
    16. if (!$digits) return [];
    17. $this->_dfs($digits, 0);
    18. return $this->res;
    19. }
    20. private function _dfs($digits, $step = 0) {
    21. if ($step == strlen($digits)) {
    22. $this->res[] = $this->str;
    23. return;
    24. }
    25. $key = substr($digits, $step, 1);
    26. $chars = $this->array[$key];
    27. foreach ($chars as $v) {
    28. $this->str .= $v;
    29. $this->_dfs($digits, $step + 1);
    30. $this->str = substr($this->str, 0, strlen($this->str) - 1);
    31. }
    32. }
    33. }
    34. $cls = new Solution();
    35. $cls->letterCombinations('23');
    36. print_r($cls->res);