PHP按多种方式排序Multi-Tired ARRAY?

时间:2014-06-02 22:57:31

标签: php

我有一个运动得分的PHP数组,我想按以下排序

赢得描述,关系描述,得分描述,允许得分Asc

谁知道我怎么能这样做?

这个数组是由多个mysql quires生成的

[14] => Array
    (
        [title] => Crushers
        [team_color] => #0000ff
        [wins] => 0
        [losses] => 1
        [ties] => 1
        [pointsscored] => 18
        [pointsallowed] => 19
    )

[15] => Array
    (
        [title] => Purple Stars
        [team_color] => #ae25d9
        [wins] => 1
        [losses] => 1
        [ties] => 0
        [pointsscored] => 20
        [pointsallowed] => 21
    )

[16] => Array
    (
        [title] => Lighting Boltz
        [team_color] => #d6c128
        [wins] => 0
        [losses] => 0
        [ties] => 2
        [pointsscored] => 12
        [pointsallowed] => 12
    )

[17] => Array
    (
        [title] => The Black Cheetahs
        [team_color] => #000000
        [wins] => 1
        [losses] => 0
        [ties] => 1
        [pointsscored] => 15
        [pointsallowed] => 13
    )

1 个答案:

答案 0 :(得分:0)

我创建了一个函数来按其列之一对每个多维数组进行排序,查看array_multisort()并根据需要自定义此函数:

 function sortArrayBy($array , $column_name,$sort=SORT_DESC){

    foreach ($array as $key => $row) {
      $column[$key]  = $row[$column_name];
    }

    array_multisort($column, $sort, $array);
    return $array;

}

这样称呼:

<?php sortArrayBy($yourArray,'date') ; ?>