pdug175 Posted April 13, 2007 Share Posted April 13, 2007 $letter_q = array("1","2","w","s","a"); $letter_w = array("q","2","3","e","s","a"); $letter_e = array("w","3","4","r","d","s"); $letter_r = array("e","4","5","t","f","d"); (...etc) for ( $i = 0; $i < array size; $i ++) { $array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); // split string into array // switch statements for each letter in alphabet switch ($array[$k]) { case "q": $current_array = $letter_q; break; case "w": $current_array = $letter_w; break; case "e": $current_array = $letter_e; break; case "r": $current_array = $letter_r; break; (....etc) } I'm creating a script that will create typos from a string. Above is a cut down extraction from my code, basically depending on the character extracted during each loop determines what that character will be substituted by. I need the above for loop to run a varying amount of times, ie depeing on the size of the array. What is the best way to acheive this, given that the array (and its size) isn't know until the loop has started? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/46854-variable-loop-length/ Share on other sites More sharing options...
MadTechie Posted April 13, 2007 Share Posted April 13, 2007 you lost me but i think you mean this <?php foreach($letter_q as $l_q) { echo $l_q."<br>" } ?> Quote Link to comment https://forums.phpfreaks.com/topic/46854-variable-loop-length/#findComment-228375 Share on other sites More sharing options...
anthylon Posted April 13, 2007 Share Posted April 13, 2007 Yes, MadTechie has right. If you want to know size of array - number of members of array there is function count. Let's make one example: $arr = array('val1', 'val2', 'val3'); while ($i=0; $i<count($arr); $i++){ echo "Member [$i] has value: <strong>${arr[$i]}</strong><br>"; } But, I like MadTechie's way more. It indepentendt what do you want to make with your code (what kind of control). I hope this was usefull for you. Quote Link to comment https://forums.phpfreaks.com/topic/46854-variable-loop-length/#findComment-228389 Share on other sites More sharing options...
pdug175 Posted April 13, 2007 Author Share Posted April 13, 2007 Thanks for the input, I've pasted the full code to avoid confusion. The 'current array' is only determined once the loop has begun, and only then is the size of that array known. I'm looping six times but this isn't right because of the variable length of the arrays. function missedkeys() { global $stringlength,$str; $newstring .= "("; // -------- Assign 'near' keywords into arrays -------- // // q -> p // $letter_q = array("1","2","w","s","a"); $letter_w = array("q","2","3","e","s","a"); $letter_e = array("w","3","4","r","d","s"); $letter_r = array("e","4","5","t","f","d"); $letter_t = array("r","5","6","y","g","f"); $letter_y = array("t","6","7","u","h","g"); $letter_u = array("y","7","8","i","j","h"); $letter_i = array("u","8","9","o","k","j"); $letter_o = array("i","9","0","p","l","k"); $letter_p = array("o","0","-","[",";","l"); // a -> l // $letter_a = array("q","w","s","x","z"); $letter_s = array("a","w","e","d","x","z"); $letter_d = array("s","e","r","f","c","x"); $letter_f = array("d","r","t","g","v","c"); $letter_g = array("f","t","y","h","b","v"); $letter_h = array("g","y","u","j","n","b"); $letter_j = array("h","u","i","k","m","n"); $letter_k = array("j","i","o","l","m"); $letter_l = array("k","o","p",";",".",","); // z -> m // $letter_z = array("a","s","x",); $letter_x = array("z","s","d","c"); $letter_c = array("x","d","f","v"); $letter_v = array("c","f","g","b"); $letter_b = array("v","g","h","n"); $letter_n = array("b","h","j","m"); $letter_m = array("n","j","k",","); for ( $k = 0; $k < $stringlength; $k ++) { for ( $i = 0; $i < 6; $i ++) { $array = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY); // split string into array // switch statements for each letter in alphabet switch ($array[$k]) { case "q": $current_array = $letter_q; break; case "w": $current_array = $letter_w; break; case "e": $current_array = $letter_e; break; case "r": $current_array = $letter_r; break; case "t": $current_array = $letter_t; break; case "y": $current_array = $letter_y; break; case "u": $current_array = $letter_u; break; case "i": $current_array = $letter_i; break; case "o": $current_array = $letter_o; break; case "p": $current_array = $letter_p; break; case "a": $current_array = $letter_a; break; case "s": $current_array = $letter_s; break; case "d": $current_array = $letter_d; break; case "f": $current_array = $letter_f; break; case "g": $current_array = $letter_g; break; case "h": $current_array = $letter_h; break; case "j": $current_array = $letter_j; break; case "k": $current_array = $letter_k; break; case "l": $current_array = $letter_l; break; case "z": $current_array = $letter_z; break; case "x": $current_array = $letter_x; break; case "c": $current_array = $letter_c; break; case "v": $current_array = $letter_v; break; case "b": $current_array = $letter_b; break; case "n": $current_array = $letter_n; break; case "m": $current_array = $letter_m; break; } $array[$k] = $current_array[$i]; // // print for ( $j = 0; $j < count($current_array); $j ++) { $newstring .= $array[$j]; // add single characters to each other } if ($i == (count($current_array))) { break; // break from last loop to eliminate final comma } $newstring .= ","; // add words to each other seperated by commas //$i .= count($current_array); } } $newstring .= ")"; return $newstring; } Quote Link to comment https://forums.phpfreaks.com/topic/46854-variable-loop-length/#findComment-228401 Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 there's gotta be a better way of building your switch statement... something like: <?php switch($array[$k]){ case $array[$k]: $current_array = $letter_{array[$k]}; break; } ?> i'm not sure how you do it, but i know there's a way. maybe have a look at variable variables. Quote Link to comment https://forums.phpfreaks.com/topic/46854-variable-loop-length/#findComment-228468 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.