liamthebof Posted August 26, 2008 Share Posted August 26, 2008 Hello. I have a file like so: freddy,382372 bobby,2312 georgy,32456 franky,48278 I now need to read this file, and output the names and values in descending order but only the top 3, ie freddy = $382372 franky = $48278 georgy = $32456 Dont worry about the =$, I can do that. Also, the file can be chnage however you like, aslong as a name and value is on each line, eg. '382372 - freddy' as I actually write the file in a previous script which can be edited. So, how can I ouput the data in dessceding order from the data give in the file. I was thinking looping max() and pregmatchign but I think im way off mark. Thanks. Link to comment https://forums.phpfreaks.com/topic/121463-solved-max-value-from-a-file/ Share on other sites More sharing options...
Mchl Posted August 26, 2008 Share Posted August 26, 2008 Use explode() to split every row into an array, then store the data in array like this one $example = array( "freddy" => 382371. "bobby" => 2312, ); Sort the array descending and output three first rows... Link to comment https://forums.phpfreaks.com/topic/121463-solved-max-value-from-a-file/#findComment-626389 Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 In practice: <?php $file = file('text.txt'); foreach ($file as $line) { $row = explode(', ', $line); $data[$row[0]] = $row[1]; } print_r($data); ?> Link to comment https://forums.phpfreaks.com/topic/121463-solved-max-value-from-a-file/#findComment-626404 Share on other sites More sharing options...
liamthebof Posted August 26, 2008 Author Share Posted August 26, 2008 Thanks for both of the replies, usign asort with the above, the job was done. Link to comment https://forums.phpfreaks.com/topic/121463-solved-max-value-from-a-file/#findComment-626434 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.