我有一個php數組作爲結果集來自數據庫。基於給定字符串的數組排序
,就像我跟MySQL的排序不太熟悉,我要作爲排序依據的一個特定的字符串我的結果集說「madhapur」
$arr=array(
array("name" => 'madhapur',"population" =>'1000'),
array("name" => 'jubiliee hills',"population" =>'800'),
array("name" => 'madhapur',"population" =>'900'),
array("name" => 'adikmet',"population" =>'200'),
array("name" => 'sr nagar',"population" =>'3000'),
array("name" => 'jubilee hills',"population" =>'1200'),
array("name" => 'madhapur',"population" =>'1000')
);
我期待的結果如下
$arr=array(
array("name" => 'madhapur',"population" =>'1000'),
array("name" => 'madhapur',"population" =>'1000'),
array("name" => 'madhapur',"population" =>'900'),
array("name" => 'adikmet',"population" =>'200'),
array("name" => 'jubilee hills',"population" =>'1200'),
array("name" => 'jubiliee hills',"population" =>'800'),
array("name" => 'sr nagar',"population" =>'3000'),
);
我嘗試使用usort,但所有這些都用於降序或升序排序。
function sortByName($a,$b){
return $b['name'] - $a['name'];
}
usort($arr,'sortByName');
所以,唯一的限制是,數組用'name'爲'madhapur'都要在一開始吧? –
是基於名稱 – saikiran
爲什麼不用MySQL對它進行排序?應該更快 –