gamefreak13 Posted May 27, 2008 Share Posted May 27, 2008 I have hundreds of sentences (one per line) and need to sort them all alphabetically and numerically. And not just for the first letter.. I want it to sort it as far as possible. Example input: What was the last drink you had? What was the last movie you watched? What was the last 11.. just inserting a number here as an example? What was the last book you read? Example output: What was the last 11.. just inserting a number here as an example? What was the last book you read? What was the last drink you had? What was the last movie you watched? This is my code I am using right now to read my text file and output it on my page (which is formatted as a MySQL INSERT query). <?php $file = "sentences.txt"; $fp = fopen($file, "r"); $data = fread($fp, filesize($file)); fclose($fp); $output = explode("\n", $data); foreach($output as $var) { if($var == "") { echo "<br>"; } else { echo "INSERT INTO survey SET question='$var';<br>"; } } echo "Done!"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/107438-solved-sorting-sentences-alphanumerically/ Share on other sites More sharing options...
msimonds Posted May 27, 2008 Share Posted May 27, 2008 Try the sort function Quote Link to comment https://forums.phpfreaks.com/topic/107438-solved-sorting-sentences-alphanumerically/#findComment-550715 Share on other sites More sharing options...
gamefreak13 Posted May 27, 2008 Author Share Posted May 27, 2008 Can you be a little more specific? I added this under $output = explode... and I get "Warning: Invalid argument supplied for foreach() in readtxt.php on line 17". sort($output); foreach($output as $key => $val) { $output = "output[" . $key . "] = " . $val . "\n"; } Quote Link to comment https://forums.phpfreaks.com/topic/107438-solved-sorting-sentences-alphanumerically/#findComment-550719 Share on other sites More sharing options...
.josh Posted May 27, 2008 Share Posted May 27, 2008 that's because inside your foreach statement you're reassigning something to $output (that's not an array) this works fine: <?php $file = "sentences.txt"; $fp = fopen($file, "r"); $data = fread($fp, filesize($file)); fclose($fp); $output = explode("\n", $data); sort($output); foreach($output as $var) { if($var == "") { echo "<br>"; } else { echo "INSERT INTO survey SET question='$var';<br>"; } } echo "Done!"; ?> it outputs: INSERT INTO survey SET question='What was the last 11.. just inserting a number here as an example?'; INSERT INTO survey SET question='What was the last book you read?'; INSERT INTO survey SET question='What was the last drink you had?'; INSERT INTO survey SET question='What was the last movie you watched?'; Done! Quote Link to comment https://forums.phpfreaks.com/topic/107438-solved-sorting-sentences-alphanumerically/#findComment-550736 Share on other sites More sharing options...
gamefreak13 Posted May 27, 2008 Author Share Posted May 27, 2008 Thanks.. I tried adding just the sort part and it worked but I didn't realize it because it outputted a bunch of empty lines so the page appeared to be blank to me. I tried it again after reading your post and noticed the scrollbar. Doh! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/107438-solved-sorting-sentences-alphanumerically/#findComment-550806 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.