0

I am trying this code but it doesn't work IN PHP


$query_s = "SELECT * FROM count_downloads_v1";
$stmt_s = $pdo->query($query_s);
$usery_s = $stmt_s->fetchAll(PDO::FETCH_ASSOC);
$arr = [];
foreach ($usery_s as $num_downloads){
    array_push($arr, $num_downloads["downloads"]);
}
$rank = $arr;
rsort($rank);
foreach($arr as $sort) {
    echo $sort. ' is rank ' . (array_search($sort, $rank) + 1) . '</br>';
    echo $sort;
}

I want to rank numbers from from largest to smallest in PHP code like (193, 188, 54, 43, 12)

Dharman
  • 26,923
  • 21
  • 73
  • 125

1 Answers1

0

Your output seems like that:

12 is rank 5
12154 is rank 3
154100 is rank 4
10012 is rank 5
12290 is rank 1
290250 is rank 2
250

You should delete the line that repeats your number:

foreach($arr as $sort) {
    echo $sort. ' is rank ' . (array_search($sort, $rank) + 1) . '</br>';
    // echo $sort;
}

If it is necessary that the output is written in descending order:

$rank = $arr;
rsort($rank);

foreach($rank as $rankKey => $value) {
  echo $value . ' is rank ' . $rankKey + 1 . '</br>';
}

But good way is use ORDER BY in MySql

rodny_c
  • 36
  • 2