Jump to content

how to write code to read and sort an array?


sphj413

Recommended Posts

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>

 

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.

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.