sniperscope Posted December 13, 2011 Share Posted December 13, 2011 Hi. I have a multidimensional array and i need sort it by two different key. I've tried almost every sort function but no chance. Array ( [0] => Array ( [0] => 1 [id] => 1 [11] => this is test [main_txt] => this is test [12] => 943965420 [add_time] => 943965420 [13] => 1323356400 [add_date] => 1323356400 ) [1] => Array ( [0] => 3 [id] => 3 [11] => another test [main_txt] => another test [12] => 943965120 [add_time] => 943965120 [13] => 1323702000 [add_date] => 1323702000 ) [2] => Array ( [0] => 32 [id] => 32 [11] => oppss one more test [main_txt] => oppss one more test [12] => 943944900 [add_time] => 943944900 [13] => 1323702000 [add_date] => 1323702000 ) [3] => Array ( [0] => 5 [id] => 5 [11] => okay, this is last one. Seriously [main_txt] => okay, this is last one. Seriously [12] => 943937160 [add_time] => 943937160 [13] => 1323615600 [add_date] => 1323615600 ) ) I need sort by add_date(Newer to Older) first then add_time(Later to Earlier) later. I hope i made my self clear. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/ Share on other sites More sharing options...
trq Posted December 13, 2011 Share Posted December 13, 2011 You forgot to post your code. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297404 Share on other sites More sharing options...
sniperscope Posted December 13, 2011 Author Share Posted December 13, 2011 Dear Thorpe The php code was simple. I've tried usort, ksort, asort....etc None of them was worked. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297406 Share on other sites More sharing options...
trq Posted December 13, 2011 Share Posted December 13, 2011 That doesn't help us help you. usort be fine, post us what you have tried. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297409 Share on other sites More sharing options...
sniperscope Posted December 13, 2011 Author Share Posted December 13, 2011 If you are asking created function? then asnwer will be No. I did not do/write any codes besides using standard sorting function of php. I will be glad if you guide me and/or give me a logical idea. I can do rest... Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297410 Share on other sites More sharing options...
kicken Posted December 13, 2011 Share Posted December 13, 2011 usort() is the function you want. It requires a callback which will compare the two array elements. Seeing as you said you already tried it, I can only assume you attempted to make that callback function. Post what you tried and we can help you fix it. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297423 Share on other sites More sharing options...
sniperscope Posted December 13, 2011 Author Share Posted December 13, 2011 usort requires user-defined comparison function. The problem starts right here. Because i don't have any function. (previously i used, usort($arr); ) As matter of fact, i have no idea logic of comparison function. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297425 Share on other sites More sharing options...
kicken Posted December 13, 2011 Share Posted December 13, 2011 The problem starts right here. Because i don't have any function. (previously i used, usort($arr); ) You have to create the function. Ex: function mySortCmp($a, $b){ ///..do any comparisons here //..return -1 if $a is less than $b //..return 0 if $a is the same as $b //..return 1 if $a is greater than $b } usort($arr, 'mySortCmp'); PHP will call your function several times, each time it will pass two elements of the array ($a and $b). Your function has to compare them and return a value which is either less than 0, 0, or greater than 0...depending on how $a compares to $b. So, create a function which will compare your elements, first by the add_date then by the add_time. Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297427 Share on other sites More sharing options...
sniperscope Posted December 13, 2011 Author Share Posted December 13, 2011 Thanks for information. You wrote "first by the add_date then by the add_time." Also i mention this in my first post. But there is a problem such as, ID | Add Date | Add Time ---+-------------------+------------ 1 | 2011/12/12 | 12:50 2 | 2011/12/12 | 14:38 3 | 2011/12/11 | 13:00 4 | 2011/12/10 | 10:53 in my logic entry #4 will be output as first record. Don't you think? Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297428 Share on other sites More sharing options...
PFMaBiSmAd Posted December 13, 2011 Share Posted December 13, 2011 I need sort by add_date(Newer to Older) first then add_time(Later to Earlier) later Both fields are DESC and apparently from a database (you are using mysql_fetch_array and getting both the numerical and associative keys.) Why not just do this in the query - ORDER BY add_date DESC, add_time DESC Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297457 Share on other sites More sharing options...
Winstons Posted December 13, 2011 Share Posted December 13, 2011 Did you tried use 'array_multisort' ? http://ua.php.net/manual/en/function.array-multisort.php Quote Link to comment https://forums.phpfreaks.com/topic/253067-sorting-multidimensional-array/#findComment-1297470 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.