PHP Sorting Help

Soldato
Joined
30 Nov 2005
Posts
3,084
Location
London
Trying to sort the first value ($val[0]), whilst keeping the second ($val[2]) in the right order along side it, using rsort (highest to lowest), but it just won't work. It seems to be ordering them in a group, for example I have put 3, 4, 300, 200 and 100 in as test values.

It displays them in this order: 4 - 3 - 300 - 200 - 100 using rsort.

Any ideas?

PHP:
$arr = file("test.txt"); 
rsort($arr); 
foreach($arr as & $val) 
{ 
    $val = explode("||", $val); 
} 
unset($val); 

echo '<table>'; 
$count = 0;
foreach ($arr as $val) 
{ 
    echo '<tr><td class="init"><strong>N:</strong>&nbsp;&nbsp;</td><td>'.$val[2].'</td>'; 
    echo '<td class="ssc">&nbsp;<strong>S:</strong>&nbsp;&nbsp;</td><td>'.$val[0].'</td>'; 
    echo '<tr><td colspan="2">&nbsp;</td></tr>'; 
	  $count++;  
	  if ($count == 5) break;
} 
echo '</table>';
 
From looking at it and just off the top of my head, I would only try to sort the array after you've exploded it otherwise you're just from the looks of it trying to rsort a string - ie. the file contents which I'm guessing are something like:

3||4||300||200||100

are just loaded into $arr as a string, and not an array as you've not exploded the string yet.
 
Back
Top Bottom