Jump to content

Using an array to sort a list of numbers


LanceT

Recommended Posts

Alright so I'm using PHP to generate a number of scores.

After the score is calculated (my script does this), I have a variable called "score."

I'm generating several of these scores and I want to sort them from highest to lowest and then print them out highest to lowest. I'm guessing I would use a PHP array and then use the sort method.

Anyone have some syntax of how I would do this?
Link to comment
https://forums.phpfreaks.com/topic/36418-using-an-array-to-sort-a-list-of-numbers/
Share on other sites

sort() will sort ascending, rsort will sort descending, which is apparently what you want. And, yes, you can just use $Array[] = $score to have the $score added to the array as a new item.

[code]<?php
$scores = 12;
$scores = 8;
$scores = 3;
$scores = 22;
$scores = 19;
$scores = 5;

rsort($scores);
print_r(scores);
?>[/code]

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.