mstne Posted November 13, 2009 Share Posted November 13, 2009 ok, I have a simple text file for saving contact information, written to a text file from a form with ~ as the delimiter, and I read it through a simple explode function and format it to a table, but the list is getting longer every day, and I need the ability to alphabetize it, and don't want all the complexity of a database. here is what I have: $read = fopen("entries.txt", "r"); $record = fgets($read); echo "<table border='1'><tr style='background-color: red'><td>Last Name</td><td>First Name</td><td>Address</td><td>City</td><td>State</td><td>Zip</td>"; echo "<td>Area Code</td><td>Phone number</td></tr>"; while(!feof($read)) { $array = explode("~", $record); echo "<tr><td>{$array[0]}</td>"; echo "<td>{$array[1]}</td>"; echo "<td>{$array[2]}</td>"; echo "<td>{$array[3]}</td>"; echo "<td>{$array[4]}</td>"; echo "<td>{$array[5]}</td>"; echo "<td>{$array[6]}</td>"; echo "<td>{$array[7]}</td></tr>"; $record = fgets($read); } echo "</table>"; fclose($read); echo "<a href='addEntry.html'>Return to add entry</a>"; I am not sure if it's because I have a loop, so each line becomes an array, but all with the same name, or not. Any ideas on how to sort an array exploded from a text file? Quote Link to comment Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 Use asort() Quote Link to comment Share on other sites More sharing options...
mstne Posted November 13, 2009 Author Share Posted November 13, 2009 asort or sort will sort an array with know values, basically. Because my program reads each line as one entry, breaking it apart into an array with 8 indexes, how can that work? Essentially, I have 4,000 entries, or lines and about 200 added every day, so that leaves me with 4000 arrays with 8 indexes, ALL named $array because of the loop. the last name resides at $array[0], and the list is becoming too big to edit manually in the text file. Quote Link to comment Share on other sites More sharing options...
Alex Posted November 13, 2009 Share Posted November 13, 2009 If you want to sort the thing entirely, and not just each segment then you'll have to create an array containing all the elements at once. You can do this by using array_merge() in each iteration of your current loop, then perform asort() on that array. Then you can loop through the entire [sorted] array in a similar fashion as to how you are now to create the table structure. Quote Link to comment Share on other sites More sharing options...
mstne Posted November 14, 2009 Author Share Posted November 14, 2009 ok, concatenated fields to make two values instead of 8, fed them into an associated array as foreach($array as $key => $value), and then sorted that by key, which starts with their last name. thanks for the help. Quote Link to comment 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.