[PHP] Sort Multi-dimensional Array Based On Two Values
Posted on 21 October 2009 by Jason Grimme
Normally when working with data that needs to be sorted, one uses the most efficient way: letting the database do it for you. I was recently in a situation where that wouldn’t work, yet I still needed to do some complex sorting. Specifically, I needed to sort a 2d array by one key’s value, and then by another key’s value.
The solution was to utilize PHP’s usort function which accepts a callback function. This callback function is a custom function that returns 0 or 1 depending on how parameter A compares with parameter B. To sort by two, three, or more, you can include multiple comparison function within your callback function.
Example – Sort an array containing arrays with keys ‘timeCreated’ and ‘age’.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | // Sort by the timeCreated key in ASC order function sortByTimeCreatedAsc($a, $b) { if ($a['timeCreated'] == $b['timeCreated']) { return 0; } return ($a['timeCreated'] < $b['timeCreated']) ? 1 : -1; } // Sort by the age key in DESC order function sortByAgeDesc($a, $b) { if ($a['age']==$b['age']) { return 0; } return ($a['age'] > $b['age']) ? 1 : -1; } // The function usort calls. Sorts by time then age. function sortByTimeThenAge($a, $b) { $t = sortByAgeDesc($a, $b); return ($comp==0) ? 0 - sortByTimeCreatedAsc($a, $b) : $t; } // Sort the cleverly named array multiDimensionalArray usort($multiDimensionalArray, 'sortByTimeThenAge'); |
It is important to note that usort takes a pointer to the array you give it and not a value, meaning it modifies the actual array you give it and does not return a new array. Also, if the first level of your array is associative, you can use uasort to preserve your keys.
In case it is not obvious, you can change from ascending to descending and vice-versa by switching the comparison operators around. Also, you can compare strings by using strcmp, a binary string comparison function.
1 |
It would be really neat if somebody wrote a class where you could just supply an array and an order field, and it sorted it out no matter how many levels deep you went. Perhaps if I have time some day…
Tags | PHP

Good Code bro!..
ביטוח רכב מיד
.., אך דבר בודד החסרונות הזה כאשר חבורה רשום על שאירע המעביד ולא בנוגע ל- העובד בל תמיד פוקד ביטוח רכב חגור,, ההתנהלות נוכח חברת הביטוח יכולה להתאכסן מסובכת מלת סירוב נעימה.
,., הוא יש בידו לתבוע חוברת הביטוח שמבטחת את הרכב בביטוח נוהל מחייב את ההפרש. דמות להטעיה כן, מחדש הביטוח הלאומי יש בידו לתבוע מחברת הביטוח את החזרי הגמלה המשולמת לעובד., ובכל מכוסה הנזק, נכון לעכשיו ניתן,,, שמשמעותה קבלת 25% מעלות הנזק שנגרם בעבורו,.
כמובן שלכל ירידה לפרטים ומדיניות ייתכן חיזיון שלא בהכרח יעיד מפני הכלל,, שנמחץ ב- האוטובוס תוך שהחליף מעדר רחב הגלגל. במהלך הדיון עלתה השאלה לגבי התהוות העסקתו להיות רגוע הנפגע,,,, ולכן נשאלה העברת משמעות לגבי הגדרת האוטובוס,. השופט הועמד לטובת חברת הביטוח ולסיכומה להיות נינוח פסיקה קורב לומר שבשל מעמדו התעסוקתי אין ניתן להזריע את אותם 25% מהוצאות הגמלה המשולמת באמצעות הביטוח הלאומי, ההיפך מכן ניתן לברור בין חבר קואופרטיב ובין פועלי חברה.
ביטוח חובה | ביטוח מקיף | ביטוח צד ג | ביטוח מקיף