我有一個對象數組$ routes,它包含我的項目的所有Fs_Controller_Router_Route對象。使用對象元素中數組屬性的數量對數組進行排序
每個Fs_Controller_Router_Route對象具有controller
,action
,params
(陣列),name
和alias
性質。
可以使用get方法(getAction,getController,getParams,getName和getAlias)訪問每個屬性。
正如你可以在下面的數組中所看到的,params屬性是一個數組,包含當前路線的PARAMS ..
array (size=6)
'home' =>
object(Fs_Controller_Router_Route)[3]
private '_controller' => string 'index' (length=5)
private '_action' => string 'index' (length=5)
private '_params' =>
array (size=2)
':name' => string 'Michael' (length=7)
':family_name' => string 'Corleone' (length=4)
private '_name' => string 'home' (length=4)
private '_alias' => string '/:name/:family_name' (length=19)
'login' =>
object(Fs_Controller_Router_Route)[4]
private '_controller' => string 'index' (length=5)
private '_action' => string 'login' (length=5)
private '_params' =>
array (size=0)
empty
private '_name' => string 'login' (length=5)
private '_alias' => string '/login' (length=6)
'signup' =>
object(Fs_Controller_Router_Route)[5]
private '_controller' => string 'index' (length=5)
private '_action' => string 'signup' (length=6)
private '_params' =>
array (size=2)
':serial' => string 'dh3kddooo' (length=9)
':token' => string '304888030' (length=9)
private '_name' => string 'signup' (length=6)
private '_alias' => string '/signup/:token/:serial' (length=22)
'about' =>
object(Fs_Controller_Router_Route)[7]
private '_controller' => string 'index' (length=5)
private '_action' => string 'index' (length=5)
private '_params' =>
array (size=2)
':serial' => string 'dh3kddooo' (length=9)
':token' => string '304888030' (length=9)
private '_name' => string 'about' (length=5)
private '_alias' => string '/about/:serial/:token' (length=6)
我想要做的,是將對象ascendantly排序,使用每個對象的參數數量。
我沒能做到這一點使用此代碼。我根本不知道如何計數PARAMS部分
array_multisort(array_map('count', $routes), SORT_ASC, $routes);
你有沒有考慮過使用'usort',只是寫一個簡單的比較功能? http://www.php.net/manual/de/function.usort.php –
我嘗試使用array_multisort(因爲我習慣於這樣做),我沒有考慮usort,非常感謝......我沒有想到它。 +1 – SmootQ