eval.in

Paste #16145

PHP — PHP 5.4.6, pasted 1 year ago

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
$data = array(
		0 => array(
				'id' => 1,
				'team' => 'Manchester United',
				'goals' => 210,
				'country' => 'england',
				'players' => 10
		),
		1 => array(
				'id' => 2,
				'team' => 'Manchester City',
				'goals' => 108,
				'country' => 'england',
				'players' => 12
		),
		2 => array(
				'id' => 3,
				'team' => 'Liverpool',
				'goals' => 108,
				'country' => 'england',
				'players' => 15
		)
);

	usort($data, function ($a, $b) {
		if ($a['goals'] == $b['goals'])
			return $a['players'] > $b['players'] ? - 1 : 1;
		return ($a['goals'] > $b['goals']) ? -1 : 1;
	});
	
	print_r($data);

?>

Program Output

Array
(
    [0] => Array
        (
            [id] => 1
            [team] => Manchester United
            [goals] => 210
            [country] => england
            [players] => 10
        )

    [1] => Array
        (
            [id] => 3
            [team] => Liverpool
            [goals] => 108
            [country] => england
            [players] => 15
        )

    [2] => Array
        (
            [id] => 2
            [team] => Manchester City
            [goals] => 108
            [country] => england
            [players] => 12
        )

)

OK (0.008 sec real, 0.012 sec wall, 13 MB, 380 syscalls)

Fork