agent0 Posted April 23, 2010 Share Posted April 23, 2010 Hello. I am trying to find a php script that will read the contents of a flat file and then sort according to a certain value in the line. The file contents appear like this: string1 tab string2 tab string3 tab string4 string1 tab string2 tab string3 tab string4 etc Tab is a tabbed space between the values in the line. I am looking for a php script to read each line into a multidimensional array and sort it according to the string2 value and then print it into a table. I have searched google for days now and can't seem to find any. I am also a noob at php so any help is greatly appreciated. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/199523-need-help-with-flat-file/ Share on other sites More sharing options...
litebearer Posted April 23, 2010 Share Posted April 23, 2010 Might take a look here... http://www.webmasterworld.com/forum88/2720.htm Quote Link to comment https://forums.phpfreaks.com/topic/199523-need-help-with-flat-file/#findComment-1047222 Share on other sites More sharing options...
teamatomic Posted April 23, 2010 Share Posted April 23, 2010 Make an associative array and use multisort $array = array( array('first' => 'Tom', 'last' => 'Edmunds'), array('first' => 'Alex', 'last' => 'Castle'), array('first' => 'Marshall', 'last' => 'Albreght')); foreach ($array as $key => $row) { $last[$key] = $row['last']; } array_multisort($last, SORT_ASC, $array); echo "<pre>"; print_r($array); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/199523-need-help-with-flat-file/#findComment-1047308 Share on other sites More sharing options...
Psycho Posted April 23, 2010 Share Posted April 23, 2010 <?php //define the input file $file = "somefile.txt"; //Read the file into an array $line = file($file); //Process the file array into a multi-dimensional array $data = array(); foreach($lines as $line) { $data = explode("\t", $line); } //Function to sort the array by index 1 funtion sortTheArray($a, $b) { if($a[1]==$b[1]) { return 0; } return ($a[1] > $b[1]) ? 1 : -1; } //Sort the array usort($data, "sortTheArray"); //Create output //======================== //Detemine columns $columns = count($data[0]); //Open table echo "<table>\n"; //Column headers echo " <tr>\n"; for($col=0; $col<$columns; $col++) { echo " <th>Field {$col}</th>\n"; } echo " <tr>\n"; //output records foreach($data as $record) { echo " <tr>\n"; foreach($record as $field) { echo " <td>{$field}</td>\n"; } echo " <tr>\n"; } //Close table echo "</table>\n"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/199523-need-help-with-flat-file/#findComment-1047316 Share on other sites More sharing options...
teamatomic Posted April 23, 2010 Share Posted April 23, 2010 Decided to make a snip for my library make the flat file like so, the first line is the fields, that way you have a value to sort by and it will work for any number of fields. first last gender status tom james male adult joe blow male minor alice jones female adult jammer hammer male adult Here is the function, at the bottom is the usage. Fairly easy to use and can be sorted by any field. Mods would be easy for adding a sort type or delimiter. function sort_array($file_name,$sorter) { $lines=file("$file_name"); $k=array_shift($lines); $k=trim($k); $kk=explode("\t",$k); $kc=0; for($kc;$kc<=count($kk)-1;$kc++) { $keys[$kc]=$kk[$kc]; } $i=0; foreach($lines as $line) { $line=trim($line); $a1=explode("\t",$line); $j=0; for($j;$j<=count($a1)-1;$j++) { $array[$i][$keys[$j]]=$a1[$j]; } $i++; } foreach ($array as $key => $row) { $sorted[$key] = $row[$sorter]; } array_multisort($sorted, SORT_ASC, $array); return $array; } $fn='./test.txt'; // val1 is the file name/path // val2 is the field to sort by $sorted_array = sort_array($fn,'last'); echo "<pre>"; print_r($sorted_array); HTH Teamatomic Quote Link to comment https://forums.phpfreaks.com/topic/199523-need-help-with-flat-file/#findComment-1047335 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.