ted_chou12 Posted January 14, 2007 Share Posted January 14, 2007 [code]function explode_lines($filename){$lines = file($filename);foreach($lines as $num => $line)$lines[$num] = explode(";seDp#", $line);return $lines;}[/code]this will explode both the lines and elements of the txt file, but what I wish to put in is the sort function, my txt file looks like:[code]title1;seDp#subject1title2;seDp#subject2title3;seDp#subject3[/code]sort by title is easy, because is the first element. therefore just:[code]function explode_lines($filename){$lines = file($filename);sort($lines);foreach($lines as $num => $line)$lines[$num] = explode(";seDp#", $line);return $lines;}[/code]but sort by subject would be harder and involves explodes...I got to:[code]function explode_lines($filename){$lines = file($filename);foreach($lines as $line) {$date = $contents[5]; $id = $contents[0];$contents = explode(";seDp#",$line); $times[] = "$date => $id";}rsort($times);foreach($times as $line)$contents = explode(" => ",$line);$line = $contents[1];echo $line[0];$lines[$line] = explode(";seDp#", $line);return $lines;}[/code]so far, but I am really stuck...Can anyone help me out?ThanksTed Link to comment https://forums.phpfreaks.com/topic/34088-exploding-an-sort/ Share on other sites More sharing options...
ted_chou12 Posted January 14, 2007 Author Share Posted January 14, 2007 can anyone help me with this please?Ted Link to comment https://forums.phpfreaks.com/topic/34088-exploding-an-sort/#findComment-160458 Share on other sites More sharing options...
GingerRobot Posted January 14, 2007 Share Posted January 14, 2007 Well if you only have the two bits of info that you are using here(titles and subjects) then id use the keys and values of a single array to store the information.i used a textfile just to test this withc;seDp#ba;seDp#ab;seDp#ccontained within it. Here is the code i used.[code]<?php$contents = file_get_contents('info.txt');$lines = explode("\n",$contents);$titles_subjects = array();foreach($lines as $lines){ $split = explode(";seDp#",$lines); $titles_subjects[$split[0]] = $split[1];}//the key stores the titles, and the value stores the subjectsksort($titles_subjects);//sort by key for title sortingreset($titles_subjects);echo 'Sorted by titles:<br/><table><th>Title</th><th>Subject</th>';foreach($titles_subjects as $output){ echo '<tr><td>'.key($titles_subjects).'</td><td>'.$output.'</td></tr>'; next($titles_subjects);}echo '</table><br /><br />';//with thanks to crudo at pinknet dot cz (PHP site for the sort function) for this method of sorting an array without loosing the keys - switches keys and values, sorts by key, then switches back. Normal sort function replcaes keys with numerical values.$temp=array_flip($titles_subjects);ksort($temp);$titles_subjects=array_flip($temp);reset($titles_subjects);echo 'Sorted by subjects:<br/><table><th>Title</th><th>Subject</th>';foreach($titles_subjects as $output){ echo '<tr><td>'.key($titles_subjects).'</td><td>'.$output.'</td></tr>'; next($titles_subjects);}echo '</table>';?>[/code] Link to comment https://forums.phpfreaks.com/topic/34088-exploding-an-sort/#findComment-160510 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.