[英]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文件中
and the score that is being posted is "score: 4" I want the php to put it in this posistion: 并且发布的分数是“分数:4”,我希望php将其放入这种情况:
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.