aebstract Posted April 28, 2010 Share Posted April 28, 2010 array: $indexH foreach ( $indexH[$var] as $val ){ array holds id, regid, eventid, q1, q2, q3, bq Is there a way to take each item from $indexH[$var] and reorder their index based on bq ordering from least to greatest? Is there some sort of order function? Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/ Share on other sites More sharing options...
Baez Posted April 28, 2010 Share Posted April 28, 2010 http://ca3.php.net/manual/en/function.sort.php php.net has every function listed along with all of it's uses. Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050009 Share on other sites More sharing options...
aebstract Posted April 28, 2010 Author Share Posted April 28, 2010 I can look at a normal sort all day and not know how to apply it to something that needs to be sorted in a more specialized way. Array ( [4.70 Index] => Array ( [0] => Array ( [firstname] => Scott [lastname] => Underwood [q1] => 4.730 [q2] => 4.706 [q3] => 4.706 [regid] => 28 [eventid] => 1 [bq] => 4.706 ) [1] => Array ( [firstname] => Randy [lastname] => Alexander [q1] => 4.822 [q2] => 4.755 [q3] => 4.755 [regid] => 119 [eventid] => 1 [bq] => 4.755 ) [2] => Array ( [firstname] => Mike [lastname] => Ghee [q1] => 99.999 [q2] => 5.045 [q3] => 5.045 [regid] => 120 [eventid] => 1 [bq] => 5.045 ) [3] => Array ( [firstname] => Kenny [lastname] => Acree [q1] => 4.730 [q2] => 4.730 [q3] => 4.730 [regid] => 117 [eventid] => 1 [bq] => 4.730 ) [4] => Array ( [firstname] => Jody [lastname] => Voyles [q1] => 99.999 [q2] => 4.973 [q3] => 4.973 [regid] => 123 [eventid] => 1 [bq] => 4.973 ) [5] => Array ( [firstname] => Jeremy [lastname] => Ellis [q1] => 5.044 [q2] => 5.044 [q3] => 5.044 [regid] => 114 [eventid] => 1 [bq] => 5.044 ) [6] => Array ( [firstname] => Jeff [lastname] => Wilkerson [q1] => 5.026 [q2] => 4.902 [q3] => 4.902 [regid] => 217 [eventid] => 1 [bq] => 4.902 ) [7] => Array ( [firstname] => Isreal [lastname] => Deraney [q1] => 4.794 [q2] => 4.732 [q3] => 4.732 [regid] => 122 [eventid] => 1 [bq] => 4.732 ) [8] => Array ( [firstname] => Blake [lastname] => Wilder [q1] => 4.685 [q2] => 5.161 [q3] => 5.023 [regid] => 121 [eventid] => 1 [bq] => 4.685 ) ) ) With this array being $indexH, my $var = Index 4.70. I'm trying to change their order in Index 4.70, the [0],[1],[2], etc. Based on the value of the bq field in that array. Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050016 Share on other sites More sharing options...
Ken2k7 Posted April 28, 2010 Share Posted April 28, 2010 There is the tag that you can use. It'll prevent the breaking of indexes. It's confusing to read your post like that, you know? Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050025 Share on other sites More sharing options...
aebstract Posted April 28, 2010 Author Share Posted April 28, 2010 Array ( [4.70 Index] => Array ( [0] => Array ( [firstname] => Kenny [lastname] => Acree [q1] => 4.730 [q2] => 4.730 [q3] => 4.730 [regid] => 117 [eventid] => 1 [bq] => 4.730 ) [1] => Array ( [firstname] => Blake [lastname] => Wilder [q1] => 4.685 [q2] => 5.161 [q3] => 5.023 [regid] => 121 [eventid] => 1 [bq] => 4.685 ) [2] => Array ( [firstname] => Jeremy [lastname] => Ellis [q1] => 5.044 [q2] => 5.044 [q3] => 5.044 [regid] => 114 [eventid] => 1 [bq] => 5.044 ) [3] => Array ( [firstname] => Randy [lastname] => Alexander [q1] => 4.822 [q2] => 4.755 [q3] => 4.755 [regid] => 119 [eventid] => 1 [bq] => 4.755 ) [4] => Array ( [firstname] => Scott [lastname] => Underwood [q1] => 4.730 [q2] => 4.706 [q3] => 4.706 [regid] => 28 [eventid] => 1 [bq] => 4.706 ) [5] => Array ( [firstname] => Isreal [lastname] => Deraney [q1] => 4.794 [q2] => 4.732 [q3] => 4.732 [regid] => 122 [eventid] => 1 [bq] => 4.732 ) [6] => Array ( [firstname] => Jody [lastname] => Voyles [q1] => 99.999 [q2] => 4.973 [q3] => 4.973 [regid] => 123 [eventid] => 1 [bq] => 4.973 ) [7] => Array ( [firstname] => Mike [lastname] => Ghee [q1] => 99.999 [q2] => 5.045 [q3] => 5.045 [regid] => 120 [eventid] => 1 [bq] => 5.045 ) [8] => Array ( [firstname] => Jeff [lastname] => Wilkerson [q1] => 5.026 [q2] => 4.902 [q3] => 4.902 [regid] => 217 [eventid] => 1 [bq] => 4.902 ) ) ) Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050027 Share on other sites More sharing options...
Ken2k7 Posted April 28, 2010 Share Posted April 28, 2010 Not tested. Just demonstrating something. <?php function compare ($a, $b) { if (!array_key_exists('bq', $a) || !array_key_exists('bq', $b)) return -1; if ($a['bq'] == $b['bq']) return 0; return $a['bq'] < $b['bq'] ? 1 : -1; // switch to reverse the ordering } foreach ($arr as $key => $value) { uasort($arr[$key], 'compare'); } Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050033 Share on other sites More sharing options...
aebstract Posted April 28, 2010 Author Share Posted April 28, 2010 This orders based on bq, but it's moving them out of their array's main name to another. foreach ($indexH[$var] as $key => $value) { uasort($indexH[$var], 'compare'); } $index contains 4 arrays, set by $var. I also ran in to an error, which is odd. After it ran through the first array of the 4, it cut the script short and produced: Fatal error: Cannot redeclare compare() (previously declared in /home/orsca/public_html/ladders.php:183) in /home/orsca/public_html/ladders.php on line 183 Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050046 Share on other sites More sharing options...
Ken2k7 Posted April 28, 2010 Share Posted April 28, 2010 What do you mean? Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050064 Share on other sites More sharing options...
aebstract Posted April 28, 2010 Author Share Posted April 28, 2010 Can't post the full array due to it being too long, but. It's moving people out of their index in to another. So this array has 4: 4.70 index, 5.30 index, 6.00 index, 7.00 index. My final output was: Array ( [4.70 Index] => Array ( [7] => Array ( [firstname] => Mike [lastname] => Ghee [q1] => 99.999 [q2] => 5.045 [q3] => 5.045 [regid] => 120 [eventid] => 1 [bq] => 5.045 ) [2] => Array ( [firstname] => Jeremy [lastname] => Ellis [q1] => 5.044 [q2] => 5.044 [q3] => 5.044 [regid] => 114 [eventid] => 1 [bq] => 5.044 ) [6] => Array ( [firstname] => Jody [lastname] => Voyles [q1] => 99.999 [q2] => 4.973 [q3] => 4.973 [regid] => 123 [eventid] => 1 [bq] => 4.973 ) [8] => Array ( [firstname] => Jeff [lastname] => Wilkerson [q1] => 5.026 [q2] => 4.902 [q3] => 4.902 [regid] => 217 [eventid] => 1 [bq] => 4.902 ) [3] => Array ( [firstname] => Randy [lastname] => Alexander [q1] => 4.822 [q2] => 4.755 [q3] => 4.755 [regid] => 119 [eventid] => 1 [bq] => 4.755 ) [5] => Array ( [firstname] => Isreal [lastname] => Deraney [q1] => 4.794 [q2] => 4.732 [q3] => 4.732 [regid] => 122 [eventid] => 1 [bq] => 4.732 ) [0] => Array ( [firstname] => Kenny [lastname] => Acree [q1] => 4.730 [q2] => 4.730 [q3] => 4.730 [regid] => 117 [eventid] => 1 [bq] => 4.730 ) [4] => Array ( [firstname] => Scott [lastname] => Underwood [q1] => 4.730 [q2] => 4.706 [q3] => 4.706 [regid] => 28 [eventid] => 1 [bq] => 4.706 ) [1] => Array ( [firstname] => Blake [lastname] => Wilder [q1] => 4.685 [q2] => 5.161 [q3] => 5.023 [regid] => 121 [eventid] => 1 [bq] => 4.685 ) ) ) Fatal error: Cannot redeclare compare() (previously declared in /home/orsca/public_html/ladders.php:183) in /home/orsca/public_html/ladders.php on line 183 Not just the error, but the people are being put in to 4.70 when they are in other classes. Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050071 Share on other sites More sharing options...
aebstract Posted April 28, 2010 Author Share Posted April 28, 2010 I think I was mistaken and this was correct, but what about the error? Fatal error: Cannot redeclare compare() (previously declared in /home/orsca/public_html/ladders.php:183) in /home/orsca/public_html/ladders.php on line 183 It appears once it gets through the first array "4.70 Index" and moves on to the next "5.30 Index". Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050082 Share on other sites More sharing options...
Ken2k7 Posted April 29, 2010 Share Posted April 29, 2010 Just rename the function to something other than compare and make the necessary edit in the call to that function. Quote Link to comment https://forums.phpfreaks.com/topic/200061-order-array-based-on-value-inside-it/#findComment-1050497 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.