如何在php中对关联数组进行排序

sou*_*ode 2 php mysql

我有mysql表即

id | a | b | c | d |ud_id
 1 | 20| 8 | 45| 18| 1

现在我想在php中检索该数据对数组进行排序并找到此示例中的最高字段(c = 45)

$query="SELECT `a`, `b`, `c`, `d` from table where ud_id=1";
$rs=mysql_query($query) or die(mysql_error());
$row=mysql_fetch_array($rs);


但是我如何对这个关联数组进行排序并找到最高的关键字.

Spu*_*ley 5

PHP有一大堆排序功能.

一个听起来像你想要的 asort()

见PHP手册为其他替代品,如sort(),ksort(),natsort(),usort(),和其他一些变型.还有shuffle()随机排序.

[编辑]好的,一步一步从阵列中获取最高价值:

asort($row);  //or arsort() for reverse order, if you prefer.
end($row);  //positions the array pointer to the last element.
print current($row); //prints "45" because it's the sorted highest value.
print key($row); //prints "c" because it's the key of the hightst sorted value.
Run Code Online (Sandbox Code Playgroud)

还有很多其他方法可以做到这一点.