Jump to content

exploding an sort


ted_chou12

Recommended Posts

[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#subject1
title2;seDp#subject2
title3;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?
Thanks
Ted
Link to comment
https://forums.phpfreaks.com/topic/34088-exploding-an-sort/
Share on other sites

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 with

c;seDp#b
a;seDp#a
b;seDp#c

contained 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 subjects

ksort($titles_subjects);//sort by key for title sorting
reset($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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.