Forum Moderators: coopster

Message Too Old, No Replies

problem sorting array by element

want to sort alphabetically by array element[2]

         

generic

8:57 pm on Mar 27, 2008 (gmt 0)

10+ Year Member



I'm going to cut right to the chase here. I don't know why I'm finding this is so difficult, but I need to sort a text file alphabetically based on the value of $field[2]:


$file_content = file('file.txt');
foreach($file_content as $data) {
$field = explode("¦" , $data);
$i++;
$class = ($i %2 == 0) ? 'right' : '';
echo "<li class=\"".$class."\" id=\"prod_".$field[0]."\"><p><a href=\"".$PHP_SELF."?prod=blinds&line=".$field[1]."\">".$field[2]."</a><br />\n";
echo "<a href=\"".$PHP_SELF."?prod=blinds&line=".$field[1]."\"><img src=\"images/".$field[3]."\" alt=\"".$field[2]."\" /></a></p></li>\n";
}

Can someone lend me a hand?

Thanks in advance :)

gen

coopster

9:39 pm on Mar 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Rather than try to prepare your output immediately you need to get all the data into an array in memory first then sort your array of data. After that you could loop and prepare the data for output.

generic

9:56 pm on Mar 27, 2008 (gmt 0)

10+ Year Member



I'm sorry, I'm not sure I understand what you mean. Could you break that down a bit for me?

coopster

10:08 pm on Mar 27, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Sure. First, you read the data into memory from your file either the way you are doing it now or perhaps with fgetcsv [php.net]. You can continue with the way you have it, you just have to loop over the entire array again first to break the data into arrays because of the pipe separator. Once all is in an array, you can use usort [php.net] to sort by the column you want. Now loop through your sorted data and prepare it for display.

generic

4:33 am on Mar 28, 2008 (gmt 0)

10+ Year Member



"Prepare it for display", "break into arrays"... I just don't understand what you mean. I know you want me to look at the PHP manual but you have to agree, for a coding beginner, the PHP manual is a bit verbose... lots of (seemingly) random variables, examples of functions with minimal explanation for the newbs and what not, making it difficult for a beginner to understand what is meant by what. I've read through it many, many times and I just don't understand it - gimme photoshop and watch me go lol.

Thanks anyway for trying, but I guess this subject just makes little sense to me. I guess I'll go with unordered output :)

Thanks for helping. I appreciate your time!

Cheers,

gen

coopster

7:05 pm on Mar 28, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Did you search the forums here? I found this thread and a linked thread within that explain what you are trying to do ...
[webmasterworld.com...]

generic

5:17 pm on Apr 7, 2008 (gmt 0)

10+ Year Member



Thanks cooptster. I've looked at the provided examples and I think I get what's going on, but I just can't get it to work.


function cmp($a, $b) {
$column_to_sort_on = 2;
if ($a[$column_to_sort_on] == $b[$column_to_sort_on]) return 0;
return ($a[$column_to_sort_on] < $b[$column_to_sort_on])? -1 : 1; }

usort($file_content, "cmp");
foreach($file_content as $data) {
$field = explode("¦" , $data);

The above code will re-sort the output but definitely not alphabetically based on the column I've selected. For the life of me, I can't figure out why this is so difficult.

coopster

8:14 pm on Apr 7, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Here is your problem:
usort($file_content, "cmp");

You are sorting the file data, row by row, comparing the first entire row to the second entire row, etc. right straight on through the file itself, not the columns in each row of the file. Based on how you are using your data, your 'file.txt' file looks something like this ...
id href text imgsrc 
-- ---- ---- ---------
1 cats Cats /cats.jpg
2 dogs Dogs /dogs.jpg
3 bird Bird /bird.jpg

Actually, it's more like this ...
1¦cats¦Cats¦/cats.jpg 
2¦dogs¦Dogs¦/dogs.jpg
3¦bird¦Bird¦/bird.jpg

So what you are currently doing is sorting your file comparing the first line to the second line. Then the 2nd line to the 3rd line. You need to break the file data out into your array first, then sort it. Something like this based on your initial message:
$table = array(); 
foreach($file_content as $data) {
$table[] = explode('¦', $data);
}
usort($table, "cmp");

The linked example shows before and after code that you can use to see what is happening [webmasterworld.com...]