問題描述
我有一個看起來像這樣的數組:
<前>大批([埃里克] => 數組([計數] => 10[變化] => 1)[Morten] => 數組([計數] => 8[變化] => 1))現在,數組中的鍵是我們幫助臺系統中技術人員的姓名.我正在嘗試根據 [count]
加上 [changes]
的數量對其進行排序,然后顯示它們.我嘗試使用 usort,但隨后數組鍵被索引號替換.我該如何排序并保留數組鍵?
你應該使用 uasort
來解決這個問題.
bool uasort ( array &$array ,回調$cmp_function )
這個函數對一個數組進行排序,使得數組索引保持它們的與數組元素的相關性它們與,使用用戶定義的比較函數.這主要用于排序時關聯數組,其中實際元素順序很重要.
I have an array that looks something like this:
Array ( [Erik] => Array ( [count] => 10 [changes] => 1 ) [Morten] => Array ( [count] => 8 [changes] => 1 ) )
Now, the keys in the array are names of technicians in our Helpdesk-system. I'm trying to sort this based on number of [count]
plus [changes]
and then show them. I've tried to use usort, but then the array keys are replaced by index numbers. How can I sort this and keep the array keys?
You should use uasort
for this.
bool uasort ( array &$array , callback $cmp_function )
This function sorts an array such that array indices maintain their correlation with the array elements they are associated with, using a user-defined comparison function. This is used mainly when sorting associative arrays where the actual element order is significant.
這篇關于使用子鍵值對 PHP 數組進行排序的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!