0

I have this simple football teams array:

   Array
(
    [0] => Array
        (
            [name] => MANCHESTER
            [pts] => 8
            [gd] => 5
        )

    [1] => Array
        (
            [name] => BOURNEMOUTH
            [pts] => 3
            [gd] => 2
        )

    [2] => Array
        (
            [name] => STOKE CITY
            [pts] => 2
            [gd] => 4
        )

    [3] => Array
        (
            [name] => LIVERPOOL
            [pts] => 3
            [gd] => 5
        )
    [4] => Array
        (
            [name] => ARSENAL
            [pts] => 9
            [gd] => 1
        )

)
  • name - is teams name
  • pts - total points of each team
  • gd-is goal difference of each team

I want to sort teams first by pts, then if we have the same pts, sort by gds.

sorting only by pts we have:

function sortByOrder($a, $b){
    return $a['pts'] - $b['pts'];
}
usort($this_is_my_array, 'sortByOrder');

Result array is:

ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
BOURNEMOUTH (pts:3, gd:2)
LIVERPOOL (pts:3, gd:5)
STOKE CITY (pts:2, gd:4)

But for BOURNEMOUTH & LIVERPOOL we have pts repeating, so we need to have this result:

ARSENAL (pts:9, gd:1)
MANCHESTER (pts:8, gd:5)
LIVERPOOL (pts:3, gd:5)
BOURNEMOUTH (pts:3, gd:2)
STOKE CITY (pts:2, gd:4)
1
  • @HaykMakyan (FYI : You can accept or upvote one of these answer which really helps you.) Commented Jul 10, 2015 at 6:44

3 Answers 3

3

Try as

uasort($your_array, function($a,$b){
    $c = $b['pts'] - $a['pts'];
    $c .= $b['gd'] - $a['gd'];
    return $c;
});
print_r($your_array);

Fiddle

Sign up to request clarification or add additional context in comments.

2 Comments

@Uchiha i think than with two digit counts may be problem, something as pts 11 gd 2 and pts 1 gd 12. i am wrong, i hope
@splash58 I didn't tested it yet with those values being very tight at working schedules. I'll when I'll be free. Anyways thanks for identifying
0

Try this:

    $array=array(
    array("name"=>"MANCHESTER","pts"=>"8","gd"=>"5"),
    array("name"=>"BOURNEMOUTH","pts"=>"3","gd"=>"2"),
    array("name"=>"STOKE CITY","pts"=>"2","gd"=>"4"),
    array("name"=>"LIVERPOOL","pts"=>"3","gd"=>"5"),
    array("name"=>"ARSENAL","pts"=>"9","gd"=>"1")
    );
  foreach ($array as $key => $row) {
    $searchcountvalue[$key]  = $row['name'];
    $pastsearchcountvalue[$key] = $row['pts'];
    $abstract_count[$key] = $row['gd'];   
}array_multisort($pastsearchcountvalue, SORT_DESC, $abstract_count,  SORT_DESC, $array);
echo "<pre>";print_r($array);

Comments

0

Your array in this format, Try check this solution.

    $s[] = array
        (
            'name' => 'MANCHESTER',
            'pts' => 8,
            'gd' => 5
        );

$s[] = array
        (
            'name' => 'BOURNEMOUTH',
            'pts' => 3,
            'gd' => 2
        );      
$s[] = array
        (
            'name' => 'STOKE CITY',
            'pts' => 2,
            'gd' => 4
        );

   $s[] = array
        (
            'name' => 'testing',
            'pts' => 2,
            'gd' => 6
        );

$s[] = array
        (
            'name'=> 'LIVERPOOL',
            'pts' => 3,
            'gd' => 5
        );      

$s[] = array
        (
            'name' => 'ARSENAL',
            'pts' => 9,
            'gd' => 1
        );





 function array_orderby()
{
    $args = func_get_args();
    $data = array_shift($args);
    foreach ($args as $n => $field) {
        if (is_string($field)) {
            $tmp = array();
            foreach ($data as $key => $row)
                $tmp[$key] = $row[$field];
            $args[$n] = $tmp;
            }
    }
    $args[] = &$data;
    call_user_func_array('array_multisort', $args);
    return array_pop($args);
}




$sorted = array_orderby($s, 'pts', SORT_DESC, 'gd', SORT_DESC);

// OutPut

Array
(
    [0] => Array
        (
            [name] => ARSENAL
            [pts] => 9
            [gd] => 1
        )

    [1] => Array
        (
            [name] => MANCHESTER
            [pts] => 8
            [gd] => 5
        )

    [2] => Array
        (
            [name] => LIVERPOOL
            [pts] => 3
            [gd] => 5
        )

    [3] => Array
        (
            [name] => BOURNEMOUTH
            [pts] => 3
            [gd] => 2
        )

    [4] => Array
        (
            [name] => Testing
            [pts] => 2
            [gd] => 6
        )

    [5] => Array
        (
            [name] => STOKE CITY
            [pts] => 2
            [gd] => 4
        )

)

1 Comment

@HaykMakyanv can you tell me what is your requirement..because i already tested

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.