php排序多维数组,但索引从1开始

时间:2023-01-27 21:34:17

I have a multidimensional array and I want to sort based off the 'rating'... but I also want the index to start at 1 which is what I used when manually creating these. When I use usort it rearranges the array but now they start at index 0 so I can't do a loop from 1 - 6 because 6 is undefined after sort.

我有一个多维数组,我想根据“评级”排序……但我也希望索引从1开始,这是我手动创建这些时所使用的。当我使用usort时,它会重新排列数组,但现在它们从索引0开始,所以我不能做一个从1到6的循环,因为6在排序后没有定义。

It was cleaner to start my $array at 1 so $player[$i] represented that actual player number. Here's what my array looks like before sort

在1美元的时候开始我的$array是比较干净的,所以$player ($i)代表了实际的播放器号码。这是排序之前的数组

$player[1]['rating'] = 8
$player[2]['rating'] = 5
$player[3]['rating'] = 10

Here's my sort function:

这是我的排序功能:

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

And I call it by

我把它叫做

usort($player, 'sortByRating');

2 个解决方案

#1


3  

The easiest way is to add this code after usort:

最简单的方法是在usort之后添加这个代码:

array_unshift($player,'temp');
unset($player[0]);

Full code:

完整的代码:

function sortByrating($a, $b) {
    return $a['rating'] - $b['rating'];
}

$player[1]['rating'] = 8;
$player[2]['rating'] = 5;
$player[3]['rating'] = 10;

usort($player, 'sortByRating');

array_unshift($player,'temp');
unset($player[0]);

print_r($player);

Output:

输出:

Array
(
    [1] => Array
        (
            [rating] => 5
        )

    [2] => Array
        (
            [rating] => 8
        )

    [3] => Array
        (
            [rating] => 10
        )

)

UPDATE with posible solution:

更新与可能的解决方案:

function sortByrating($a, $b) {
    return $a['rating'] - $b['rating'];
}

$player[1]['rating'] = 8;
$player[2]['rating'] = 5;
$player[3]['rating'] = 10;

uasort($player, 'sortByRating');

foreach($player as $player_id=>$player_data) {
    $place++;
    $player[$player_id]['place'] = $place;
    $places[$place] = $player_id;
    echo "Player #{$player_id} takes Place #{$place}\n";
}

echo "\nPlayers array: ";
print_r($player);

echo "\nPlaces array: ";
print_r($places);

Ouput:

输出:

Player #2 takes Place #1
Player #1 takes Place #2
Player #3 takes Place #3

Players array: Array
(
    [2] => Array
        (
            [rating] => 5
            [place] => 1
        )

    [1] => Array
        (
            [rating] => 8
            [place] => 2
        )

    [3] => Array
        (
            [rating] => 10
            [place] => 3
        )

)

Places array: Array
(
    [1] => 2
    [2] => 1
    [3] => 3
)

#2


1  

Just simply add the following after your usort:

只需在您的usort清单后添加以下内容:

$player = array_combine(range(1, count($player)), $player);

uasort() can be used such that the array keeps the original keys:

uasort()可以使用这样的数组保存原始键:

uasort($player, function ($x, $y) { 
    return $x['rating'] - $y['rating']; 
});

#1


3  

The easiest way is to add this code after usort:

最简单的方法是在usort之后添加这个代码:

array_unshift($player,'temp');
unset($player[0]);

Full code:

完整的代码:

function sortByrating($a, $b) {
    return $a['rating'] - $b['rating'];
}

$player[1]['rating'] = 8;
$player[2]['rating'] = 5;
$player[3]['rating'] = 10;

usort($player, 'sortByRating');

array_unshift($player,'temp');
unset($player[0]);

print_r($player);

Output:

输出:

Array
(
    [1] => Array
        (
            [rating] => 5
        )

    [2] => Array
        (
            [rating] => 8
        )

    [3] => Array
        (
            [rating] => 10
        )

)

UPDATE with posible solution:

更新与可能的解决方案:

function sortByrating($a, $b) {
    return $a['rating'] - $b['rating'];
}

$player[1]['rating'] = 8;
$player[2]['rating'] = 5;
$player[3]['rating'] = 10;

uasort($player, 'sortByRating');

foreach($player as $player_id=>$player_data) {
    $place++;
    $player[$player_id]['place'] = $place;
    $places[$place] = $player_id;
    echo "Player #{$player_id} takes Place #{$place}\n";
}

echo "\nPlayers array: ";
print_r($player);

echo "\nPlaces array: ";
print_r($places);

Ouput:

输出:

Player #2 takes Place #1
Player #1 takes Place #2
Player #3 takes Place #3

Players array: Array
(
    [2] => Array
        (
            [rating] => 5
            [place] => 1
        )

    [1] => Array
        (
            [rating] => 8
            [place] => 2
        )

    [3] => Array
        (
            [rating] => 10
            [place] => 3
        )

)

Places array: Array
(
    [1] => 2
    [2] => 1
    [3] => 3
)

#2


1  

Just simply add the following after your usort:

只需在您的usort清单后添加以下内容:

$player = array_combine(range(1, count($player)), $player);

uasort() can be used such that the array keeps the original keys:

uasort()可以使用这样的数组保存原始键:

uasort($player, function ($x, $y) { 
    return $x['rating'] - $y['rating']; 
});