在php中将多维数组从高到低排序

时间:2022-03-30 21:35:51

25Hi I have checked on here and cant find a similar result so if this has been asked I do apologies.

25Hi我在这里检查过,无法找到类似的结果,所以如果有人问我会道歉。

Anyway, I am generating an multi array which has been generated by a nice little class.

无论如何,我正在生成一个由一个漂亮的小班生成的多数组。

Anyway the array comes like this when output:

无论如何,输出时数组都是这样的:

array(2047) {
    [0]=> array(6) {
        ["name"]=> string(4) "name"
        ["gamesplayed"] => string(2) "26"
        ["points"] => string(2) "26"
    }
    [1]=> array(6) {
        ["name"]=> string(4) "name"
        ["gamesplayed"] => string(2) "26"
        ["points"] => string(2) "42"
    }
    [2]=> array(6) {
        ["name"]=> string(4) "name"
        ["gamesplayed"] => string(2) "26"
        ["points"] => string(2) "16"
    }
    // and so on
}

My question is... Is there an easy php function or a neat way on sorting the array into high to low using the points value?

我的问题是......是否有一个简单的PHP函数或使用点值将数组从高到低排序的简洁方法?

I have had a look at a few php functions but they are just for standard arrays with a single value.

我看过几个PHP函数,但它们只适用于具有单个值的标准数组。

Thanks for any help given :)

谢谢你给予的任何帮助:)

UPDATE

UPDATE

I have tried this and it does not sort by points.

我试过这个并没有按点分类。

Here is the class I am doing it with:

这是我正在做的课程:

//create sort value
private function sorting_table($a, $b){
    return $a['points'] - $b['points'];
}

//sort all users into a table and order the highest to the lowest
public function output_prediction_table($pdo, $year){

    //get all uers and put in an array with games played and points.
    $getallusers = $pdo->prepare("SELECT userid FROM userpredictions WHERE year=:year");
    $getallusers->execute(array(':year' => $year));

    //create the multidimentional array
    $multidimentional_array = array();

    while($fetchallusers = $getallusers->fetch()){

        $multidimentional_array[] = array(
            'name' => $this->get_username($pdo, $fetchallusers['userid']),
            'games' => $this->get_games_played($pdo, $year),
            'points' => $this->make_points($pdo, $year, $fetchallusers['userid']),
            'userid' => $fetchallusers['userid']
        );

    }

    usort($multidimentional_array, array($this, 'sorting_table'));

    $output = '<table class="table-striped">';
    $output .= '<tr class="t-h"><td class="t-c">Position</td><td>Username</td><td>Games Played</td><td>Points</td></tr>';
    $tablenumber = 1;

    foreach($multidimentional_array as $newarray){

        $output .= '<tr><td class="t-c">'.$tablenumber.'</td><td><a href="/user/'.$this->sluggify($newarray["name"]).'/'.$newarray["userid"].'/" title="View '.ucwords($newarray["name"]).' Profile">'.ucwords($newarray["name"]).'</a></td><td>'.$newarray["games"].'</td><td>'.$newarray["points"].'</td></tr>';
        $tablenumber++;

    }

    $output .= '</table>';

    return $output;

}

I am not getting any errors, its just not sorting with the highest points first to the lowest points last.

我没有得到任何错误,它只是没有排在最高点,最后到最低点。

2 个解决方案

#1


1  

Try usort

尝试使用

You can do something like this:

你可以这样做:

usort($array, 'sortByPoints');

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

Yo can find more examples here: Sort Multi-dimensional Array by Value

Yo可以在这里找到更多示例:按值排序多维数组

#2


0  

Using usort Here is complete and tested code:

使用usort这是完整且经过测试的代码:

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

$test = array(
    0=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "26"
    ),
    1=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "42",
    ),
    2=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "16",
    )
);

usort($test, 'sortByOrder');
print_r($test);

#1


1  

Try usort

尝试使用

You can do something like this:

你可以这样做:

usort($array, 'sortByPoints');

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

Yo can find more examples here: Sort Multi-dimensional Array by Value

Yo可以在这里找到更多示例:按值排序多维数组

#2


0  

Using usort Here is complete and tested code:

使用usort这是完整且经过测试的代码:

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

$test = array(
    0=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "26"
    ),
    1=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "42",
    ),
    2=> array(
        "name"=> "name",
        "gamesplayed" => "26",
        "points" => "16",
    )
);

usort($test, 'sortByOrder');
print_r($test);