shedokan Posted July 7, 2008 Share Posted July 7, 2008 I have a table in the database wich stores some numbers, then I calculate those numbers and combine them. then I take all the numbers and the combined number and put them into an array wich stores more numbers and more combined numbers in different id's, now I want to sort the combined numbers wich are in a multi array, the array now looks like this: <?php Array ( [1] => Array ( [num1] => 1 [num2] => 1.5 [num3] => 1.125 [combinedNum] => 3.625 ) [2] => Array ( [num1] => 1.5 [num2] => 0.875 [num3] => 0.875 [combinedNum] => 3.25 ) [3] => Array ( [num1] => 13.5 [num2] => 2.375 [num3] => 3.625 [combinedNum] => 19.5 ) ) ?> what is the simplest way of sorting this array by the combinedNum? thanks, I really appreciate your knowledge. Link to comment https://forums.phpfreaks.com/topic/113638-how-can-i-arrange-an-array-i-got-from-database/ Share on other sites More sharing options...
shedokan Posted July 7, 2008 Author Share Posted July 7, 2008 please, I found the function array_multisort but I don't know how to use it. Link to comment https://forums.phpfreaks.com/topic/113638-how-can-i-arrange-an-array-i-got-from-database/#findComment-584005 Share on other sites More sharing options...
roopurt18 Posted July 7, 2008 Share Posted July 7, 2008 You can't change your database query to have an ORDER BY column and just get the data out of the database already sorted? That's usually the easiest and most efficient method. If you insist on the array_multisort() method, you want to model your code on example #3 in the array_multisort documentation. <?php $combinedNum = array(); foreach( $your_array as $key => $val ) { $combinedNum[$key] = $val['combinedNum']; } array_multisort( $combinedNum, SORT_ASC, $your_array ); ?> Link to comment https://forums.phpfreaks.com/topic/113638-how-can-i-arrange-an-array-i-got-from-database/#findComment-584018 Share on other sites More sharing options...
shedokan Posted July 8, 2008 Author Share Posted July 8, 2008 thanks, and I can't use ORDER BY because I calculate those numbers into different numbers. Link to comment https://forums.phpfreaks.com/topic/113638-how-can-i-arrange-an-array-i-got-from-database/#findComment-584370 Share on other sites More sharing options...
roopurt18 Posted July 8, 2008 Share Posted July 8, 2008 You do know that you can calculate data in the MySQL query and ORDER BY calculated fields, right? Link to comment https://forums.phpfreaks.com/topic/113638-how-can-i-arrange-an-array-i-got-from-database/#findComment-584565 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.