Php 一些字符串的排序?

Php 一些字符串的排序?,php,Php,我只能想象,这是相当简单的,但解决办法我想不起来 假设我有以下变量: $group1 = "5"; $group2 = "1"; $group3 = "15"; $group4 = "3"; $group5 = "7"; $group6 = "1"; $group7 = "55"; $group8 = "0"; $group9 = "35"; 我希望先列出金额最高的组,例如: Group 7 is number 1 with 55. Group 9 is number 2 with 35. G

我只能想象,这是相当简单的,但解决办法我想不起来

假设我有以下变量:

$group1 = "5";
$group2 = "1";
$group3 = "15";
$group4 = "3";
$group5 = "7";
$group6 = "1";
$group7 = "55";
$group8 = "0";
$group9 = "35";
我希望先列出金额最高的组,例如:

Group 7 is number 1 with 55.
Group 9 is number 2 with 35.
Group 3 is number 3 with 15.
Group 5 is number 4 with 7.
Group 1 is number 5 with 5.
Group 4 is number 6 with 3.
Group 2 is number 7 with 1.
Group 6 is number 8 with 1.
Group 8 is number 9 with 0.

也许在一个双数组中列出所有数据并对其排序会更容易些?

是的,使用数组是最好的做法。 诸如此类

$group[1]="5";
$group[2]="1";

之后,您可以对数组进行排序

将组放入数组中

$groups = array("5","1","15","3","7","1","55","0","35");
arsort($groups); //This sort the array is descending order

var_dump($sorted_groups);
要以您的格式打印阵列,请使用以下函数

count = 1;
foreach($groups as $key => $value) {
    echo "Group ".($key+1)." is number ".$count++." with ".$value;
}

首先,使用数组只是普通的数组

如果你的数组是

$group = array(1 => 5, 2 => 1 ... )
您可以使用arsort函数

这里我使用数字,而不是字符串。如果要对值使用字符串,则需要为sort\u NUMERIC设置一个标志

更多信息请访问

然后使用foreach


为此,请使用数组

$group[1] = "5";
$group[2] = "1";
$group[3] = "15";
$group[4] = "3";
$group[5] = "7";
$group[6] = "1";
$group[7] = "55";
$group[8] = "0";
$group[9] = "35";
然后分类

arsort($group, SORT_NUMERIC);   // SORT_NUMERIC suggested by **fab**

最好的方法是使用数组和arsort。这将保持索引的完整性


arsort返回一个布尔值,因此不分配给新变量

$groups = array("5","1","15","3","7","1","55","0","35");
arsort($groups, SORT_NUMERIC);

$i = 1;

foreach ($groups as $key => $val) {
    echo 'Group ' . $key . ' is number ' . $i . ' with ' . $val;
    $i++;
}

只需将数据放在关联数组中,并使用关联感知排序对其进行排序:

$groups = array(
'group1' => "5",
'group2' => "1",
'group3' => "15",
'group4' => "3",
'group5' => "7",
'group6' => "1",
'group7' => "55",
'group8' => "0",
'group9' => "35",
);

arsort($groups);

// iteration as usual
foreach ($groups as $group_name => $value) {
}

// getting elements with the array functions based around the array's internal pointer
reset($groups); // reset the pointer to the start
print key($groups); // the first element's key
print current($groups); // the first element's value
next($groups); // moving the array to the next element

带有数字的变量几乎总是坏代码的标志。使用数组,而不是双数组,不管你是什么意思。感谢大家给出了这么多答案,但我感觉你们中的一些人忽略了这个事实,groupID需要跟在数字后面。列出没有相应组的所有数字将毫无意义。+1对于使用appropiate排序函数而言。但是您也忘记了排序标志:sort_numeric这将是最简单的解决方案-但是不要忘记,如果您使用字符串作为值,那么排序将是字符串,而不是基于数字的。在尝试使用值进行排序之前,您需要将值强制转换为数字类型:+1用于使用适当的排序功能。但是您也忘记了排序标志:sort\u NUMERIC$group将被布尔值覆盖在本例中,排序函数只是对数组进行排序并返回true或falsearsort返回布尔值,因此不要分配给新变量这对我来说最有意义,因为groupID跟在数字后面,但我如何访问关联数组?假设数组按这里的方式排序,我只想打印数字最大的组。通常我会使用类似$group[0]的东西,假设数组已排序,但对于关联数组,我没有指向目标的起点。或者是吗?有一系列函数基于数组的内部指针,比如key、current、reset、next等。如果您不想简单地使用foreach通过数组进行迭代,这些函数会有所帮助。增加了一些例子。
$groups = array(
'group1' => "5",
'group2' => "1",
'group3' => "15",
'group4' => "3",
'group5' => "7",
'group6' => "1",
'group7' => "55",
'group8' => "0",
'group9' => "35",
);

arsort($groups);

// iteration as usual
foreach ($groups as $group_name => $value) {
}

// getting elements with the array functions based around the array's internal pointer
reset($groups); // reset the pointer to the start
print key($groups); // the first element's key
print current($groups); // the first element's value
next($groups); // moving the array to the next element