简体   繁体   English

PHP将数字从高到低排序?

[英]PHP put Numbers into Order highest to lowest?

So my Php gets Javascript vars and then sends them to a file called LeaderBoard.txt What I want the Php to do is when it sends the information to the .txt file I want it so the scores are sorted from highest to lowest... 所以我的Php获取Javascript变量,然后将它们发送到一个名为LeaderBoard.txt的文件中,我希望Php要做的是将信息发送到我想要的.txt文件中,以便将分数从最高到最低排序...

so if in the .txt file there is 因此,如果在.txt文件中

  1. score: 5 得分:5
  2. score: 3 得分:3

and the score that is being posted is "score: 4" I want the php to put it in this posistion: 并且发布的分数是“分数:4”,我希望php将其放入这种情况:

  1. score: 5 得分:5
  2. score: 4 得分:4
  3. score: 3 得分:3

Iv no idea how to do it so how could i do it? 我不知道该怎么做,那我该怎么办? and how can i implement it into my existing code? 以及如何将其实现到现有代码中? Thanks for reading :) and heres my php so far: 感谢您的阅读:)和到目前为止我的PHP:

<?php 

$hiScore = $_POST['hiScore'] ? $_POST['hiScore'] : 'not set';

$theInput = $_POST['theInput'] ? $_POST['theInput'] : 'not set';

$file = fopen('LeaderBoard.txt','a+');
fwrite($file, ' Name: '.$theInput.'                                        Score: '.$hiScore.'                         '.PHP_EOL);
fclose($file);


?> 

I'm a really newby with php and its a stuggle so sorry if this seems a silly question 我是php的新手,它很费力,如果这看起来很愚蠢,对不起

I ended up doing it like this: 我最终这样做是这样的:

<?php 

$hiScore = $_POST['hiScore'] ? $_POST['hiScore'] : 'not set';

$theInput = $_POST['theInput'] ? $_POST['theInput'] : 'not set';

$file = fopen('LeaderBoard.txt','a+');
fwrite($file, ' '.$hiScore.' - Score                                                      Name: '.$theInput.'      '.PHP_EOL);
fclose($file);

$lines = file("LeaderBoard.txt");
natsort($lines);
$lines=array_reverse($lines);
file_put_contents("LeaderBoardScores.txt", implode("\n  \n", $lines));
?> 

Use ksort ... ksort is used to sort an array element from lowest to highest. 使用ksort ... ksort用于将数组元素从最低到最高排序。 You can use other sorting functions in php through the below link. 您可以通过以下链接在php中使用其他排序功能。 enter link description here 在此处输入链接说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 查找字符串中的最高和最低数字? - Finding the highest and lowest numbers in a string? 给定n个数字,显示最高和最低数字 - Given n numbers, show the highest and lowest number 返回一串带空格的数字中的最高和最低数字 - Return highest and lowest number in a string of numbers with spaces 在不使用 sort() 的情况下,在 javascript 中从高到低对数字进行排序 - sort numbers from highest to lowest in javascript without using sort() Javascript,从最高到最低对数字数组进行排序,但保留原始索引 - Javascript, Sort an array of numbers from highest to lowest but keep original index 如何按降序(从高到低)Javascript 进行冒泡排序? - How to bubble sort in descending order (HIGHEST to Lowest) Javascript? 列表<li>项目按 object 值从低到高的顺序排列</li> - list <li> items in order of object value from lowest to highest 我试图按最有价值的顺序排列数组,从最低到最高,但是那些值为 0 的数组在最高值之后排序, - I am trying to order the array by most valued, from lowest to highest, but those with 0 value are ordered after the highest valued, 如何从生成随机数的数组中确定最高和最低数字; JavaScript的 - How to determine the highest and lowest numbers from an array that generates random numbers; JavaScript 什么是最低和最高数量 - What is the lowest and highest number of
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM