booxvid Posted July 24, 2012 Share Posted July 24, 2012 This program is to sort words into qwerty order. <?php // include 'qwerty.php'; $words = array("one,two,free"); $order = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'); $key = array(); $text = "one two free"; $words = explode(" ",$text); for($i=0;$i<count($words);$i++){ $next_word = $words[$i]; $fletter_nextword = get_first_letter($next_word); $key_nextword =get_key($fletter_nextword,$order); $key[$i] = $key_nextword; } $words_key = array_combine($key, $words); ksort($words_key); foreach ($words_key as $key => $value){ echo $value.'</br>'; } function get_first_letter($this_word){ $temp = explode(' ',$this_word); return $temp[0]; } function get_key($the_word,$order){ $key=""; $i=0; do{ if($the_word==$order[$i]){ $flag = "TRUE"; $key = $i; } else{ $flag = "FALSE"; } $i++; }while($flag=="FALSE"); return $key; } ?> It says : Notice: Undefined offset: 26 in C:\xampp\htdocs\OOP_SYSTEM\qwerty sort\sort_qwerty.php on line 48 Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/ Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 Which line is 48? Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/#findComment-1363943 Share on other sites More sharing options...
booxvid Posted July 24, 2012 Author Share Posted July 24, 2012 Oh sorry, I've already updated my codes. <?php // include 'qwerty.php'; $words = array("one,two,free"); $order = array('q','w','e','r','t','y','u','i','o','p','a','s','d','f','g','h','j','k','l','z','x','c','v','b','n','m'); $key = array(); $text = "one two free"; $words = explode(" ",$text); // for($i=0;$i<count($words);$i++){ // $next_word = $words[0]; echo $next_word; $temp = array(); echo $temp[0]; //$fletter_nextword = get_first_letter($next_word); // $key_nextword =get_key($fletter_nextword,$order); // // $key[0] = $key_nextword; // echo $key[0]; // // } // $words_key = array_combine($key, $words); // ksort($words_key); // // foreach ($words_key as $key => $value){ // // echo $value.'</br>'; // } function get_first_letter($this_word){ $temp = explode(' ',$this_word); return $temp[0]; } function get_key($the_word,$order){ $key=""; for($i=0;$i<count($order);$i++){ if($the_word==$order[$i]){ $key = $i; break; } } return $key; } ?> my problem right now is how to get the first letter of a string. Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/#findComment-1363945 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 Use substr to only get the first character. function get_first_letter($this_word) { return substr($this_word, 0, 1); } Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/#findComment-1363946 Share on other sites More sharing options...
booxvid Posted July 24, 2012 Author Share Posted July 24, 2012 Thanks! I'm almost done with this sorting work. function get_key($the_letter,$order){ $key=""; for($i=0;$i<count($order);$i++){ if($the_letter==$order[$i]){ $key = $i; break; } } return $key; } got one last question: is my function correct? by when I loop in with the array of qwerty, together the first word that I've got is this the right way in stopping a for loop? Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/#findComment-1363948 Share on other sites More sharing options...
peipst9lker Posted July 24, 2012 Share Posted July 24, 2012 Yes, break is the right way to stop a loop. The function will return the index of the letter in your querty array - I don't know if that's what you want But array_search does the exact same thing, consider using that one! Link to comment https://forums.phpfreaks.com/topic/266161-undefined-offset-for-sorting-words/#findComment-1363949 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.