0

PHP多次元配列のカスタムソート。

並べ替えは、フィールドの値に基づいて行う必要があります[position] 。人は複数のポジションを持つことができます(以下にリストされている特殊なケースを参照)。

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

配列を位置の値に基づいて並べ替えたい

  1. 書記長
  2. 大統領
  3. 会計
  4. 秘書
  5. 委員

以下の配列の特殊なケース では、最初の配列である必要があります。この人は「書記長」だから

[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
    )
)
4

2 に答える 2

1

了解しました。これを確認します。

usort()車輪の再発明の代わりに引き続き使用できます。のような位置で2人を比較するだけ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
        )

)
于 2012-10-18T08:55:44.667 に答える
0

クイックソートまたはその他の並べ替えアルゴリズムを実装して、正しい変数で比較が行われるようにすることができます。

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
于 2012-10-18T07:10:57.247 に答える