目前我正在尝试根据每个项目的值来排序我的数组。现在它应该从上到下排序如下:“Platinum”,“Gold”,“Silver”,“Bronze”,“None”,“完成”,“未完成”。现在问题出在运行usort()之后函数它使数组的大小加倍。我使用的代码是:
$query = "SELECT * FROM postallions WHERE userid = :userid";
$params = array(':userid' => $userid);
try{
$stmt = $connection->prepare($query);
$result = $stmt->execute($params);
}
catch(PDOException $ex){
echo ("Failed to run query: " . $ex->getMessage());
}
$columns = $stmt->fetch();
$postallionlist = $columns;
unset($postallionlist["userid"]);
print_r($postallionlist);
var_dump($postallionlist);
function compareMedals( $a, $b ) {
$aMap = array(0 => 'None', 1 => 'Bronze', 2 => 'Silver', 3 => 'Gold', 4 => 'Platinum', 5 => 'Uncomplete', 6 => 'Complete');
$aValues = array( 0, 1, 2, 3, 4, 5, 6);
$a = str_ireplace($aMap, $aValues, $a);
$b = str_ireplace($aMap, $aValues, $b);
return $a - $b;
}
usort($postallionlist, 'compareMedals');
print_r($postallionlist);
var_dump($postallionlist);
现在当我在输出函数之前var_dump数组时:
array (size=22)
'userid' => string '1' (length=1)
0 => string '1' (length=1)
'p1' => string 'Silver' (length=6)
1 => string 'Silver' (length=6)
'p2' => string 'Platinum' (length=8)
2 => string 'Platinum' (length=8)
'p3' => string 'None' (length=4)
3 => string 'None' (length=4)
'p4' => string 'None' (length=4)
4 => string 'None' (length=4)
'p5' => string 'Bronze' (length=6)
5 => string 'Bronze' (length=6)
'p6' => string 'Gold' (length=4)
6 => string 'Gold' (length=4)
'p7' => string 'Complete' (length=8)
7 => string 'Complete' (length=8)
'p8' => string 'None' (length=4)
8 => string 'None' (length=4)
'p9' => string 'None' (length=4)
9 => string 'None' (length=4)
'p10' => string 'None' (length=4)
10 => string 'None' (length=4)
并且print_r:
Array ( [userid] => 1 [0] => 1 [p1] => Silver [1] => Silver [p2] => Platinum [2] => Platinum [p3] => None [3] => None [p4] => None [4] => None [p5] => Bronze [5] => Bronze [p6] => Gold [6] => Gold [p7] => Complete [7] => Complete [p8] => None [8] => None [p9] => None [9] => None [p10] => None [10] => None )
那些阵列都很完美。现在在函数之后它输出以下内容:(var_dump then print_r)
array (size=22)
0 => string 'None' (length=4)
1 => string 'None' (length=4)
2 => string 'None' (length=4)
3 => string 'None' (length=4)
4 => string 'None' (length=4)
5 => string 'None' (length=4)
6 => string 'None' (length=4)
7 => string 'None' (length=4)
8 => string 'None' (length=4)
9 => string 'None' (length=4)
10 => string 'Bronze' (length=6)
11 => string 'Bronze' (length=6)
12 => string '1' (length=1)
13 => string '1' (length=1)
14 => string 'Silver' (length=6)
15 => string 'Silver' (length=6)
16 => string 'Gold' (length=4)
17 => string 'Gold' (length=4)
18 => string 'Platinum' (length=8)
19 => string 'Platinum' (length=8)
20 => string 'Complete' (length=8)
21 => string 'Complete' (length=8)
Array ( [0] => None [1] => None [2] => None [3] => None [4] => None [5] => None [6] => None [7] => None [8] => None [9] => None [10] => Bronze [11] => Bronze [12] => 1 [13] => 1 [14] => Silver [15] => Silver [16] => Gold [17] => Gold [18] => Platinum [19] => Platinum [20] => Complete [21] => Complete )
那么为什么数组会在函数后加倍?