Jump to content

[SOLVED] alphabetize explode(array) from a text file


mstne

Recommended Posts

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?

Link to comment
Share on other sites

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. 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.