sphj413 Posted October 16, 2008 Share Posted October 16, 2008 original plain text file 1: A C D 4: A B 5: D F 7: A E 9: B C result file should look like this A: 1 4 7 B: 4 9 C: 1 9 D: 1 5 E: 7 F: 5 i got this far and got stuck. I was able to split out the delimeters and create the array, but i don't have a clue as to how to sort this array to get the desired results. if someone could just get me started in what's a good start on this. thanks in advance...spj413 <html> <body> <?PHP $file_handle = fopen("arrayfile.txt", "r"); while (!feof($file_handle) ) { $line_of_text = fgets($file_handle); $parts = split('[ :]', $line_of_text); //$array = array( "$parts[0] $parts[1] $parts[2] $parts[3] $parts[4]"); foreach ($parts as $key => $val) { echo " [" . $key . "] = " . $val . "<br>"; } } fclose($file_handle); ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/128641-how-to-write-code-to-read-and-sort-an-array/ Share on other sites More sharing options...
darkfreaks Posted October 16, 2008 Share Posted October 16, 2008 do this <?php $array=array( "$parts[0] $parts[1] $parts[2] $parts[3] $parts[4]"); asort($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/128641-how-to-write-code-to-read-and-sort-an-array/#findComment-666693 Share on other sites More sharing options...
.josh Posted October 16, 2008 Share Posted October 16, 2008 Here's my take: <?php // make an array of the lines of the file $list = file('arrayfile.txt'); // for each line of the file... foreach ($list as $val) { // trim the carriage returns off the end of line $val = rtrim($val, "\r\n"); // explode the line at the spaces into an array $row = explode(' ',$val); // first pos in array is the "num:" so remove the : $row[0] = substr($row[0],0,1); // make a loop to loop through each letter of the line // start at 1 because 1st array pos is the num for ($x = 1; $x < count($row); $x++) { // add the current number to the current letter's string $newlist[$row[$x]] .= $row[0] . " "; } // end for } // end foreach // $newlist is not in alphabetical order, because it is created // in order of letter appearance, so let's sort it ksort($newlist); ?> Basically what this does is first read the file into an array, making each line an element of the array. It then removes the carriage returns and the colon and breaks each the current line into another array. First position of that array is the number, all subsequent positions are the letters. Then we loop through each item of the line, starting at the first letter on the line, and ending on the last letter. Inside that for loop, basically what we are doing is producing another array where the letters are the keys and the values of each position is a string of the space separated numbers. We simply concat the current number of the row to the array element with the key named the current letter of the for loop. In other words, the loop does this: 1: A C D 4: A B 5: D F 7: A E 9: B C $newlist['A'] .= "1 "; $newlist['C'] .= "1 "; $newlist['D'] .= "1 "; $newlist['A'] .= "4 "; $newlist['B'] .= "4 "; $newlist['D'] .= "5 "; $newlist['F'] .= "5 "; $newlist['A'] .= "7 "; $newlist['E'] .= "7 "; $newlist['B'] .= "9 "; $newlist['C'] .= "9 "; Since each key of the array is the letters, the order is determined by appearance of the letters, making the resulting order A C D B F E. So throw in a ksort to put the array keys in alphabetical order, and you end up with: Array ( [A] => 1 4 7 [b] => 4 9 [C] => 1 9 [D] => 1 5 [E] => 7 [F] => 5 ) edited so late for: Sorry I wrote it late last night and forgot to really explain what it did. Quote Link to comment https://forums.phpfreaks.com/topic/128641-how-to-write-code-to-read-and-sort-an-array/#findComment-666809 Share on other sites More sharing options...
sphj413 Posted October 17, 2008 Author Share Posted October 17, 2008 thanks for all of the help i got on this. i still haven't gotten it working, but it's gotten me started. Thanks, sphj Quote Link to comment https://forums.phpfreaks.com/topic/128641-how-to-write-code-to-read-and-sort-an-array/#findComment-667734 Share on other sites More sharing options...
.josh Posted October 17, 2008 Share Posted October 17, 2008 What doesn't work? Quote Link to comment https://forums.phpfreaks.com/topic/128641-how-to-write-code-to-read-and-sort-an-array/#findComment-668120 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.