Log in

View Full Version : Help With Ordering File Data From Highest To Lowest Without Array



M2com
10-22-2011, 04:58 PM
I am trying to make a scoreboard which takes the data of each txt file in a folder and then orders all the text file's data (including the name of the file which = username). So, for example, i would look like this:

user: .... 88
anotheruser: .... 9
oneotheruser: .... 2

etc

I've been trying to use arsort() and arrays but it doesn't order it at all. Here is my script:

<?php
$directory = 'folder/';
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {

$lines = file('folder/' . $file);
$file = preg_replace('/(.*)\..*/', '$1', $file);
$fruits = array("$file" => "$lines[0]");

arsort($fruits);
foreach ($fruits as $key => $val) {
echo "$key = $val\n <br>";
}
}
}
closedir($handle);
}
?>

Thanks!

djr33
10-22-2011, 05:03 PM
arsort() keeps the array keys as they are. If you just want to reorder everything into numerical order, you can use rsort() and that will generate a new array that just lists the numbers in order (but loses the original keys/indices).

Also, if you want to check on how the script is doing as it progresses, try adding this line:
print_r($fruits); die();

Put that anywhere you would like and the output of your script will be a printout of the array then the script will stop execution. You can move that line around to different places to be sure that the array is correct at that point-- here I'd suggest trying it before and after the sort function to see if there's an unrelated problem and to see if you are actually sorting it in the way that you want.

(Obviously after testing you'll want to remove that line.)

M2com
10-22-2011, 05:06 PM
hmmm... I'm not really looking to have to use arrays but if that's the only way to make the username along with their scores go into order of highest to lowest then that's how I should do it right?

djr33
10-22-2011, 05:24 PM
Why do you want to avoid arrays? How else would you sort it? Arrays are just an ordered list of information. Any other method would be reinventing the wheel. (Of course the best option for all of this is to store your information in a database then you could just search for results in the order you want.)

M2com
10-23-2011, 04:19 AM
True but I do not have that available at the moment. hmmm....

djr33
10-23-2011, 05:22 AM
Is this an assignment? Of course you have it available in PHP. The only reason you wouldn't is if an instructor said you can't use it to try to get you to solve the problem creatively. The only other approach I can think of is successive loops, although that would get very annoying (and slow) if you needed beyond-first-letter alphabetizing. Note that usort() allows custom user functions to compare items in an array, so you could use that if you want to "do it yourself".
And since we have a policy against doing homework here (that's what it sounds like you're describing), please post a specific question if you want more help-- the idea of homework is to learn, so we won't be helping you by giving you an answer, especially not a complete answer without any of your work in it.

(If this isn't homework, explain your situation and why you can't use arrays. If you just don't have the data in an array, we can look at ways to get it from whatever format you have into an array. What is the starting point and what, precisely, is the end point?)

Or-- maybe I misread your post completely: are you saying you don't have a database available? For any complex sorting/searching/organizing operations using a database is almost always your best option. It might take a bit of work to set up (maybe even changing servers), but it will help. You can probably do a simple version of this using just arrays though.