0

What i used:

        $maxpoints = 40;
        for($i = 0; $i < count($trainergross); $i++){
                $trainergross[$i]['points'] = $maxpoints;
                $maxpoints -=2;
        }

heres my sort function:

function grossSort($gross, $compare) {
    if($gross['gross'] > $compare['gross'])
        return -1; // move up
    else if($gross['gross'] < $compare['gross'])
        return 1; // move down
    else
        return 0; // do nothing
}

This is tricky (for me at least). I need to generate a points system based on the highest value gross degrading by 2.

here is my array after running a function to sort it by highest gross ($trainergross):

Array
(
    [0] => Array
        (
            [instr] => blah
            [club] => Colvin
            [gross] => 2146
            [bud] => 0
            [ratio] => 0
        )

    [1] => Array
        (
            [instr] => rob
            [club] => Evans
            [gross] => 2000
            [bud] => 0
            [ratio] => 0
        )

    [2] => Array
        (
            [instr] => new
            [club] => Boulevard
            [gross] => 930
            [bud] => 755
            [ratio] => 248
        )

    [3] => Array
        (
            [instr] => test
            [club] => Boulevard
            [gross] => 805
            [bud] => 50
            [ratio] => 50
        )

    [4] => Array
        (
            [instr] => lee
            [club] => Boulevard
            [gross] => 235
            [bud] => 60
            [ratio] => 30
        )

    [5] => Array
        (
            [instr] => rob
            [club] => Boulevard
            [gross] => 175
            [bud] => 125
            [ratio] => 125
        )

    [6] => Array
        (
            [instr] => rob
            [club] => Henrietta
            [gross] => 60
            [bud] => 0
            [ratio] => 0
        )

)

In this example $trainergross[0] should get a key => value of "points" => 40

$trainergross[1] would get 38.. [2] would get 36... etc.

I would then usort the array by bud, then do it again increasing the value key based on highest bud.

My issue is, i dont even know where to start with doing this. Is it possible to add a key based on the order of the array?

The only thought i have is doing 3 different arrays then merging them all, but that seems terribly inefficient.

Sorry for vague, dont really even know how to ask, hopefully this explained it well enough.

5
  • how are you generating points value ? Commented Jul 26, 2013 at 17:15
  • 2
    Start by telling us why 0 gets 40 points and 1 gets 38
    – Prix
    Commented Jul 26, 2013 at 17:16
  • @prix theres no reason. its just the points system. you finish in first place (0) you get 40 points. everyone after that gets 2 less
    – robz228
    Commented Jul 26, 2013 at 17:18
  • Also, why is "points" value a string?
    – hjpotter92
    Commented Jul 26, 2013 at 17:18
  • @hjpotter92 shouldnt have been :P removed
    – robz228
    Commented Jul 26, 2013 at 17:19

1 Answer 1

2

If your array is already sorted then you can use something like this:

$maxpoints = 40;
foreach($trainergross as $trainer){
    $trainer['points'] = $maxpoints;
    $maxpoints -=2;
}

If you want to add more points based on their position in another criteria, then sort the array for next property (i.e. bud) and repeat the above but adding to the current value of points:

$trainer['points'] += $maxpoints;
3
  • 1
    Also make sure your array is truly sorted, PHP can troll you by placing [0] => {highest gross} in the middle of the array for example, read usort manual for more info.
    – kajacx
    Commented Jul 26, 2013 at 17:32
  • yikes, was that ever easy. thanks for the direction. I put what i ended up using at the bottom
    – robz228
    Commented Jul 26, 2013 at 17:37
  • @kajacx i threw the function i used on the question, does that look fine? i found something on this site and altered it to my needs
    – robz228
    Commented Jul 26, 2013 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.