我需要使用基于一个来自使用php4的mysql调用的字符串的usort来排序一个数组。如何将一个额外的变量传递到一个cmp函数中,以便在php中使用usort
到目前为止,我有mysql的呼吁得到订单:
$result=mysql_query("SELECT rank from order WHERE id=1");
$row = mysql_fetch_row($result);
这给了我像$行[0] = '阿尔贝托,卡洛斯,Brocephus,埃德加,丹妮拉';
我具备的功能,它的工作,如果我硬编码在数组中:
function cmp($a,$b){
//$order = how do I get $row[0] in here?
$a_index = array_search($a['name'], $order);
if (!$a_index) {
$a_index = 999;
}
$b_index = array_search($b['name'], $order);
if (!$b_index) {
$b_index = 999;
}
return $a_index - $b_index;
}
usort($names,cmp);
什么让这串入CMP函数作为数组的最简单的方法?
http://php.net/manual/en/function.usort.php看例子#4有何帮助? – Pitchinnate 2013-04-03 18:29:28
@Pitchinnate由于OP说他们使用的是没有关闭的PHP4,所以它不会对他的情况有所帮助。 – 2013-04-03 18:30:50
@MikeBrant没有注意到PHP4的一部分。 – Pitchinnate 2013-04-03 18:33:58