Jump to content

order array based on value inside it


aebstract

Recommended Posts

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.

Link to comment
Share on other sites

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
                )

        )

)

Link to comment
Share on other sites

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');
}

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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".

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.