PHP多维数组自定义排序

时间:2012-10-18 07:01:27

标签: php

PHP多维数组自定义排序。

排序应基于字段[position]中的值 一个人可以有多个职位(参见下面列出的特殊情况)。

Array
(
    [0] => Array
    (
        [position] => Array
            (
                [0] => Secretary
            )
    )
)

我希望数组在位置值上排序为

  1. 总书记
  2. 总统
  3. 财务主管
  4. 秘书
  5. 委员
  6. 特殊情况 对于下面的数组,它应该是第一个数组。因为这个人是“总书记”

    [2] => Array
    (
        [person_id] => 51136
        [position] => Array
            (
                [0] => General Secretary
                [1] => Committee Member
            )
    
        [person_name] => Person 1
    )
    

    示例输入数据

    Array
    (
        [0] => Array
        (
            [person_id] => 22180
            [position] => Array
                (
                    [0] => Secretary
                )
            [person_name] => Person 1
        )
    
        [1] => Array
        (
            [person_id] => 51135
            [position] => Array
                (
                    [0] => President
                )
            [person_name] => Person 2
        )
    
        [2] => Array
        (
            [person_id] => 51136
            [position] => Array
                (
                    [0] => General Secretary
                    [1] => Committee Member
                )
    
            [person_name] => Person 3
        )
    
        [3] => Array
        (
            [person_id] => 44141
            [position] => Array
                (
                    [0] => Treasurer 
                )
    
            [person_name] => Person 4
        )
    
        [4] => Array
        (
            [person_id] => 51137
            [position] => Array
                (
                    [0] => Committee Member
                )
            [person_name] => Person 5
        )
    )
    

    需要输出

    Array
    (
        [0] => Array
        (
            [person_id] => 51136
            [position] => Array
                (
                    [0] => General Secretary
                    [1] => Committee Member
                )
    
            [person_name] => Person 3
        )
        [1] => Array
        (
            [person_id] => 51135
            [position] => Array
                (
                    [0] => President
                )
            [person_name] => Person 2
        )
        [2] => Array
        (
            [person_id] => 44141
            [position] => Array
                (
                    [0] => Treasurer 
                )
    
            [person_name] => Person 4
        )
        [3] => Array
        (
            [person_id] => 22180
            [position] => Array
                (
                    [0] => Secretary
                )
            [person_name] => Person 1
        )
        [4] => Array
        (
            [person_id] => 51137
            [position] => Array
                (
                    [0] => Committee Member
                )
            [person_name] => Person 5
        )
    )
    

2 个答案:

答案 0 :(得分:1)

好吧,我说出来了。

您仍然可以使用usort()而不是重新发明轮子。只需按照array_search($position,$pos_array)等位置比较两个人。

function sortByPosition($ps)
{
    $pos=array(0=>"General Secretary",1=>"President",2=>"Treasurer",3=>"Secretary",4=>"Committee Member");
    usort($ps,function($p1,$p2) use ($pos){
        $lvl1=count($pos);
        $lvl2=count($pos);
        foreach($p1["position"] as $position)
        {
            $lvl1=min(array_search($position,$pos),$lvl1);
        }
        foreach($p2["position"] as $position)
        {
            $lvl2=min(array_search($position,$pos),$lvl2);
        }
        return $lvl1-$lvl2;
    });
    return $ps;
}

测试:

$arr=Array(Array("person_id" => 22180,"position" => Array("Secretary"),"person_name" => "Person 1"), Array("person_id" => 51135,"position" => Array("President"),"person_name" => "Person 2"), Array("person_id" => 51136,"position" => Array("General Secretary","Committee Member"),"person_name" => "Person 3"), Array("person_id" => 44141,"position" => Array("Treasurer"),"person_name" => "Person 4"), Array("person_id" => 51137,"position" => Array("Committee Member"),"person_name" => "Person 5"));
print_r(sortByPosition($arr));

输出:

Array
(
    [0] => Array
        (
            [person_id] => 51136
            [position] => Array
                (
                    [0] => General Secretary
                    [1] => Committee Member
                )

            [person_name] => Person 3
        )

    [1] => Array
        (
            [person_id] => 51135
            [position] => Array
                (
                    [0] => President
                )

            [person_name] => Person 2
        )

    [2] => Array
        (
            [person_id] => 44141
            [position] => Array
                (
                    [0] => Treasurer
                )

            [person_name] => Person 4
        )

    [3] => Array
        (
            [person_id] => 22180
            [position] => Array
                (
                    [0] => Secretary
                )

            [person_name] => Person 1
        )

    [4] => Array
        (
            [person_id] => 51137
            [position] => Array
                (
                    [0] => Committee Member
                )

            [person_name] => Person 5
        )

)

答案 1 :(得分:0)

您可以实现Quicksort或任何其他排序算法,并确保在正确的变量上进行比较。

http://en.wikipedia.org/wiki/Quicksort

从Wiipedias伪代码中你可以通过更改第7行来检查数组的位置参数。

function quicksort('array')
      if length('array') ≤ 1
          return 'array'  // an array of zero or one elements is already sorted
      select and remove a pivot value 'pivot' from 'array'
      create empty lists 'less' and 'greater'
      for each 'x' in 'array'
          if 'x' ≤ 'pivot' then append 'x' to 'less' // change to x['position'] and pivot['position']
          else append 'x' to 'greater'
      return concatenate(quicksort('less'), 'pivot', quicksort('greater')) // two recursive calls