Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 多维数组上的排名/位置_Php_Arrays_Sorting_Multidimensional Array_Ranking - Fatal编程技术网

Php 多维数组上的排名/位置

Php 多维数组上的排名/位置,php,arrays,sorting,multidimensional-array,ranking,Php,Arrays,Sorting,Multidimensional Array,Ranking,我有这样一个数组: Array ( [1] => Array ( [0] => Array ( [user_id] => 13162 [selling_points] => 110.2 [total_points] => 189.6

我有这样一个数组:

    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
            )
        )
)
我想根据卖点位置卖点和总位置最终值分配两种位置

结果应该是这样的:

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函数,但没有成功。。。有人能帮我吗

<?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,
        ],
    ];
}
现在没有时间解释,如果你想解释,明天提醒我。和/或等待更好的解决方案- 我遗漏了数组的一个嵌套级别,但这应该是微不足道的


不要在没有markPosition函数的情况下尝试,否则可能会因为foreach循环中的引用而导致意外行为。

正如我所看到的,@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);