多维数组上的排名/位置

时间:2015-02-16 14:45:23

标签: php arrays sorting multidimensional-array ranking

我有一个这样的数组:

    Array
(
    [1] => Array
        (
            [0] => Array
                (
                    [user_id] => 13162
                    [selling_points] => 110.2
                    [total_points] => 189.6
                    [contest_name] => Gold
                [position_selling] => 0
                [position_final] => 0
            )

        [1] => Array
            (
                [user_id] => 16712
                [selling_points] => 80.4
                [total_points] => 90.3
                [contest_name] => Gold
                [position_selling] => 0
                [position_final] => 0
            )
    )

[2] => Array
    (
        [0] => Array
            (
                [user_id] => 24613
                [selling_points] => 1400.72
                [total_points] => 1978.29
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        [1] => Array
            (
                [user_id] => 41317
                [selling_points] => 775.33
                [total_points] => 847
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        [2] => Array
            (
                [user_id] => 41045
                [selling_points] => 655.03
                [total_points] => 1065
                [contest_name] => Silver
                [position_selling] => 0
                [position_final] => 0
            )
        )
)

我想通过selling_points(position_selling)和total_points(position_final)的值分配2种位置。

结果应该是这样的:

Array
(
    [1] => Array
        (
            [0] => Array
                (
                [user_id] => 13162
                [selling_points] => 110.2
                [total_points] => 189.6
                [contest_name] => Gold
                [position_selling] => 1
                [position_final] => 1
            )

        [1] => Array
            (
                [user_id] => 16712
                [selling_points] => 80.4
                [total_points] => 90.3
                [contest_name] => Gold
                [position_selling] => 2
                [position_final] => 2
            )
    )

[2] => Array
    (
        [0] => Array
            (
                [user_id] => 24613
                [selling_points] => 1400.72
                [total_points] => 1978.29
                [contest_name] => Silver
                [position_selling] => 1
                [position_final] => 1
            )

        [2] => Array
            (
                [user_id] => 41045
                [selling_points] => 655.03
                [total_points] => 1065
                [contest_name] => Silver
                [position_selling] => 3
                [position_final] => 2
            )

        [3] => Array
            (
                [user_id] => 41317
                [selling_points] => 775.33
                [total_points] => 847
                [contest_name] => Silver
                [position_selling] => 2
                [position_final] => 3
            )
        )
)

我尝试使用像usort和array_multisort这样的php函数但没有成功......有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:0)

<?php
$data = getData();
$s1 = array();
foreach( $data as &$v ) {
    $s1[] = &$v;
}
$s2 = $s1;
usort( $s1, compareBy('selling_points'));
usort( $s2, compareBy('total_points'));

markPosition($s1, 'position_selling');
markPosition($s2, 'position_final');

echo '$data=', var_export($data); echo ";\r\n\r\n";


function markPosition(array $arr, $key) {
    foreach( $arr as $v=>&$k ) {
        $k[$key] = $v+1;
    }
}

function compareBy($key) {
    return function($a,$b) use($key) {
        return $b[$key] - $a[$key];
    };
}


function getData() {
    return [
        [
            'user_id' => 1,
            'selling_points' => 1,
            'total_points' => 1,
        ],
        [
            'user_id' => 2,
            'selling_points' => 4,
            'total_points' => 4,
        ],
        [
            'user_id' => 3,
            'selling_points' => 3,
            'total_points' => 5,
        ],
    ];
}

现在没时间解释它(明天提醒我,如果你想解释一下)。和/或等待更好的解决方案...... ;-)
我遗漏了你的数组的一个嵌套级别,但这应该是微不足道的。

http://docs.php.net/references
并且不要在没有markPosition()函数的情况下尝试它,否则你可能会因为foreach循环中的引用而最终出现意外行为....

答案 1 :(得分:0)

我看到@VlkerK已经给你几乎与我准备的答案相同。

$arr = [
         [
           ['user_id' => 13162,
            'selling_points' => 110.2,
            'total_points' => 189.6,
            'contest_name' => 'Gold',
            'position_selling' => 0,
            'position_final' => 0
           ],
           ['user_id' => 16712,
            'selling_points' => 80.4,
            'total_points' => 90.3,
            'contest_name' => 'Gold',
            'position_selling' => 0,
            'position_final' => 0
           ]
         ],
         [
           ['user_id' => 24613,
            'selling_points' => 1400.72,
            'total_points' => 1978.29,
            'contest_name' => 'Silver',
            'position_selling' => 0,
            'position_final' => 0
           ],
           ['user_id' => 41317,
            'selling_points' => 775.33,
            'total_points' => 847,
            'contest_name' => 'Silver',
            'position_selling' => 0,
            'position_final' => 0
           ],
           ['user_id' => 41045,
            'selling_points' => 655.03,
            'total_points' => 1065,
            'contest_name' => 'Silver',
            'position_selling' => 0,
            'position_final' => 0
           ]
         ]
        ];

$newArr = array();

function cmp($a, $b)
{
    if ($a['selling_points'] == $b['selling_points']) {
        return 0;
    }
    return ($a['selling_points'] < $b['selling_points']) ? 1 : -1;
}

function cmpTotal($a, $b)
{
    if ($a['total_points'] == $b['total_points']) {
        return 0;
    }
    return ($a['total_points'] < $b['total_points']) ? 1 : -1;
}

foreach($arr as $contest) {

    usort($contest,'cmp');
    $byTotal = $contest;
    usort($byTotal,'cmpTotal');
    $iTotal=1;
    foreach($byTotal as &$user) {
        $i = array_search($user,$contest);
        $user['position_selling'] = $i+1;
        $user['position_final'] = $iTotal++;
    }
    $newArr[] = $byTotal;
}

print_r($newArr);