rascle Posted November 19, 2012 Share Posted November 19, 2012 (edited) Hi I have an array (which is an equation) lets say it is Array ( [0] => 18x [1] => 10x^2 [2] => 17 ) I want to order the array in terms of the value after ^, so that the highest values after ^ are grouped first. Therefore the above array I want it sorted to become Array ( [0] => 10x^2 [1] => 18x [2] => 17 ) Does anyone have any idea how I would sort it? I have tried using php sort() to no avail. Thanks, Rhys Edited November 19, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/ Share on other sites More sharing options...
rascle Posted November 19, 2012 Author Share Posted November 19, 2012 I havent got a solution but I can change the values so that x is x^1 and a constant is x^0. This might make it easier, all I need to know is how to order based on the value after "x^" Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393618 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 You would have to usort() with a custom function Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393619 Share on other sites More sharing options...
rascle Posted November 19, 2012 Author Share Posted November 19, 2012 But what would the function be? Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393620 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 (edited) What have you got so far? Edited November 19, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393623 Share on other sites More sharing options...
rascle Posted November 19, 2012 Author Share Posted November 19, 2012 (edited) Using the php.net I have come up with the following, but it is probably rubbish!: function sortdtr($a,$B){ $a = explode('x^',$a); $b = explode('x^',$B); if($a==$B){ return 0; } else if($a>$B){ return -1;} else if($a<$B){ return 1; } } usort($dtr, "sortdtr"); echo "<br/>"; print_r($dtr); Edited November 19, 2012 by Zane Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393624 Share on other sites More sharing options...
rascle Posted November 19, 2012 Author Share Posted November 19, 2012 But it isn't listing them how I want it, I'll have to do some more research unless you have any insights? Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393625 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 You aren't comparing the exponent values, just the arrays. You need to compare $a[1] with $b[1] after exploding. There will also be items without "x^" Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393627 Share on other sites More sharing options...
rascle Posted November 19, 2012 Author Share Posted November 19, 2012 Solved, thank you Quote Link to comment https://forums.phpfreaks.com/topic/270914-sort-array-by-values/#findComment-1393630 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.