Jump to content

Recommended Posts

I have an array that looks like this.

 

Array

(

    [0] => stdClass Object

        (

            [id] => 1439

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 591

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-02-27

            [contract_start_date] => 2012-03-01

            [contract_exp_date] => 2012-05-30

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 15.75

            [q1Points] => 0

            [q2Points] => 21

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 21

        )

 

    [1] => stdClass Object

        (

            [id] => 3345

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 191

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-05-30

            [contract_start_date] => 2012-05-30

            [contract_exp_date] => 2012-08-28

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 21

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 21

        )

 

    [2] => stdClass Object

        (

            [id] => 3477

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 579

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-07-01

            [contract_start_date] => 2012-07-01

            [contract_exp_date] => 2012-09-29

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 20

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 20

        )

 

    [3] => stdClass Object

        (

            [id] => 1375

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 52

            [fighterOrder] => 2

            [contract_type] => 2

            [contract_date] => 2012-05-30

            [contract_start_date] => 2012-05-30

            [contract_exp_date] => 2012-08-28

            [pickup_date] => 2012-02-25

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 15

            [q1Points] => 0

            [q2Points] => 20

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 20

        )

 

    [4] => stdClass Object

        (

            [id] => 3499

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 29

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-07-16

            [contract_start_date] => 2012-07-16

            [contract_exp_date] => 2012-10-14

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 16

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 16

        )

 

    [5] => stdClass Object

        (

            [id] => 3352

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 609

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-05-30

            [contract_start_date] => 2012-05-30

            [contract_exp_date] => 2012-08-28

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 12

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 12

        )

 

    [6] => stdClass Object

        (

            [id] => 3665

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 361

            [fighterOrder] => 0

            [contract_type] => 1

            [contract_date] => 0000-00-00

            [contract_start_date] =>

            [contract_exp_date] => 0000-00-00

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 0

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 0

        )

 

    [7] => stdClass Object

        (

            [id] => 3514

            [player_id] => 11

            [league_id] => 34

            [fighter_id] => 318

            [fighterOrder] => 0

            [contract_type] => 2

            [contract_date] => 2012-07-25

            [contract_start_date] => 2012-07-25

            [contract_exp_date] => 2012-10-23

            [pickup_date] => 0000-00-00

            [drop_date] => 0000-00-00

            [reserve_date] => 0000-00-00

            [active_date] => 0000-00-00

            [changed] => 0

            [status] => 1

            [draft_round] => 0

            [playOffBonus] => 0

            [q1Points] => 0

            [q2Points] => 0

            [q3Points] => 0

            [q4Points] => 0

            [total_points] => 0

        )

 

)

 

Then I have sort code that looks like this:

            function cmp( $a, $b ) { 
                if(  $a->total_points ==  $b->total_points ){ return 0 ; } 
                return ($a->total_points > $b->total_points) ? -1 : 1;
            }

            usort($query,'cmp');

 

This works fine for the total points. Sorts them. But what if I wanted to sort by both.  I want all contract types of 2 at the top (sorted by the total points) then the contract points 1 and 3 at the bottom in whatever order. Is this possible without going to great lengths?

Link to comment
https://forums.phpfreaks.com/topic/267338-array-second-sort-parameter/
Share on other sites

try

function cmp($a, $b)
{
    if ($a['contract_type'] == $b['contract_type']) {
        return $b['total_points'] - $a['total_points'];
    }
    else {
        return ($a['contract_type']==2) ? -1 : 1;
    }
}

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.