Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The general structure you want to hold your number -> letter assignments is an array or arrays, similar to:</p> <pre><code>// 0 = N, 1 = L, 2 = T, 3 = D, 4 = R, 5 = V or F, 6 = B or P, 7 = Z, // 8 = H or CH or J, 9 = G $numberMap = new Array ( 0 =&gt; new Array("N"), 1 =&gt; new Array("L"), 2 =&gt; new Array("T"), 3 =&gt; new Array("D"), 4 =&gt; new Array("R"), 5 =&gt; new Array("V", "F"), 6 =&gt; new Array("B", "P"), 7 =&gt; new Array("Z"), 8 =&gt; new Array("H", "CH", "J"), 9 =&gt; new Array("G"), ); </code></pre> <p>Then, a bit of recursive logic gives us a function similar to:</p> <pre><code>function GetEncoding($number) { $ret = new Array(); for ($i = 0; $i &lt; strlen($number); $i++) { // We're just translating here, nothing special. // $var + 0 is a cheap way of forcing a variable to be numeric $ret[] = $numberMap[$number[$i]+0]; } } function PrintEncoding($enc, $string = "") { // If we're at the end of the line, then print! if (count($enc) === 0) { print $string."\n"; return; } // Otherwise, soldier on through the possible values. // Grab the next 'letter' and cycle through the possibilities for it. foreach ($enc[0] as $letter) { // And call this function again with it! PrintEncoding(array_slice($enc, 1), $string.$letter); } } </code></pre> <p>Three cheers for recursion! This would be used via:</p> <pre><code>PrintEncoding(GetEncoding("052384")); </code></pre> <p>And if you really want it as an array, play with output buffering and explode using "\n" as your split string.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload