简体   繁体   English

使用索引对多维数组进行排序

[英]Sort multidimensional Array with an index

I have the following array and I want to sort all the records with total in descending order. 我有以下数组,我想按降序对所有记录进行排序。 Can anyone tell me the php code or pseudo code for doing it. 任何人都可以告诉我PHP代码或伪代码。

Array
(
    [reg_id] => Array
        (
            [0] => 5
            [1] => 7
            [2] => 17
            [3] => 18
            [4] => 19
            [5] => 20
            [6] => 34
        )

    [name] => Array
        (
            [0] => employee1
            [1] => employee6
            [2] => employee3
            [3] => employee4
            [4] => employee2
            [5] => empoyee5
            [6] => employee9
        )

    [test_w] => Array
        (
            [0] => 21/30
            [1] => 15/30
            [2] => 27/30
            [3] => 16.5/30
            [4] => 21/30
            [5] => 18/30
            [6] => 12/30
        )

    [intr_w] => Array
        (
            [0] => 8/10
            [1] => 6/10
            [2] => 9/10
            [3] => 9/10
            [4] => 3.3/10
            [5] => 7/10
            [6] => 0/10
        )

    [exp_w] => Array
        (
            [0] => 2.5/5
            [1] => 4/5
            [2] => 4.35/5
            [3] => 4.5/5
            [4] => 4.8/5
            [5] => 4.5/5
            [6] => 0/5
        )

    [educ_w] => Array
        (
            [0] => 37.41/55
            [1] => 44.14/55
            [2] => 33.27/55
            [3] => 38.43/55
            [4] => 34.52/55
            [5] => 46.11/55
            [6] => 43.66/55
        )

    [total] => Array
        (
            [0] => 68.91
            [1] => 69.14
            [2] => 73.62
            [3] => 68.43
            [4] => 63.62
            [5] => 75.61
            [6] => 55.66
        )

)

Use this function should solve the problem 使用此功能应解决问题

function multisort($array, $key, $sort_flags = SORT_REGULAR) {
        if (is_array($array) && count($array) > 0) {
            if (!empty($key)) {
                $mapping = array();
                foreach ($array as $k => $v) {
                    $sort_key = '';
                    if (!is_array($key)) {
                        $sort_key = $v[$key];
                    } else {
                        // @TODO This should be fixed, now it will be sorted as string
                        foreach ($key as $key_key) {
                            $sort_key .= $v[$key_key];
                        }
                        $sort_flags = SORT_STRING;
                    }
                    $mapping[$k] = $sort_key;
                }
                asort($mapping, $sort_flags);
                $sorted = array();
                foreach ($mapping as $k => $v) {
                    $sorted[] = $array[$k];
                }
                return $sorted;
            }
        }
        return $array;     

}

For Example: 例如:

$result=multisort($array,$sort_flags = DESC);

The simplest (but least efficient) would be a simple bubble sort 最简单(但效率最低)的是简单的冒泡排序

Pseudo code 伪代码

 for( $i from 0 to count($arr[total])):
       for( $j from 0 to count(...)):
            if($arr[total][$i] < $arr[total][$j]):
                   //SWAP VALUES AT i AND j
                   $this->swap($arr[total][$i],$arr[total][$j]);
                   $this->swap($arr[name][$i],$arr[name][$j]);
                   $this->swap($arr[reg_id][$i],$arr[reg_id][$j]);
                   // etc for all required fields
           endif;
       endfor;
 endfor;

SWAP FUNCTION (PASS VALUES BY REFERENCE SO THEY WILL GET CHANGED) 交换功能(通过参考传递价值,因此它们将会改变)

 function swap(&$i, &$j){
    $t=$i;
    $i=$j;
    $j=$t;
}

Since those seem to be fields related to an entity, the best answer I can give you is to build better data structures. 由于那些似乎是与实体相关的字段,我能给你的最佳答案是建立更好的数据结构。 By example, you should build an Employee (or whatever entity you are representing there) class containing those fields: 例如,您应该构建一个包含这些字段的Employee(或您在那里代表的任何实体)类:

class Employee
{
 public $reg_id;
 public $name;
 public $test_w;
 public $intr_w;
 public $exp_w;
 public $educ_w;
 public $total;
}

and then keep an array of Employee's. 然后保留一个Employee的数组。 What if you had another field that in turn was an array? 如果你有另一个字段又是一个数组怎么办? I know my answer doesn't address the actual sorting, I'm just saying your way of keeping the data can get really dirty really quick. 我知道我的答案并没有解决实际的排序问题,我只是说你保持数据的方式可以非常快地变脏。

I have sort array with the following function. 我有一个具有以下功能的排序数组。

  function msort($array, $key_s) {
    $arraynew = $array;
    foreach($array as $key=>$val){
        if($key == $key_s){

            for($i=0; $i<count($val); $i++){
                for($j=0; $j<count($val); $j++){
                    if($val[$i] > $val[$j]){
                        $temp = $val[$i];
                        $val[$i] = $val[$j];
                        $val[$j] = $temp;

                        // swap all values
                        foreach($arraynew as $arrKey=>$arrVal){
                            $temp = $arraynew[$arrKey][$i];
                            $arraynew[$arrKey][$i] = $arraynew[$arrKey][$j];
                            $arraynew[$arrKey][$j] = $temp;
                        }
                    }
                }
            }
        }

    }
    return $arraynew;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM