3

値によって配列に頼りたい配列があります。これが既存の配列です。

  1 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "111111"
  ExpDate => "2013-07-31"
  Level => "WELITE"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null
2 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "222222"
  ExpDate => "2013-07-31"
  Level => "WELITE"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null
3 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "22222"
  ExpDate => "2013-07-31"
  Level => "WELITE"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null

配列を次のように再分類し、レベルと呼ばれる値に基づいてキーを作成しようとしています:

 WELITE  => (3)
  1 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "111111"
  ExpDate => "2013-07-31"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null
2 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "222222"
  ExpDate => "2013-07-31"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null
3 => Array (9)
  OrgID => "XXXX"
  DOB => "1970-01-01"
  PersonID => "22222"
  ExpDate => "2013-07-31"
  Status => "Active"
  FName => "Jane"
  LName => "Doe"
  Checked => null

これが私がこれまでに試したことです:

    $sort = array();
foreach($WlevelArray as $k=>$v) {
    $sort['Level'][$k] = $v['Level'];
    $sort['OrgID'][$k] = $v['OrgID'];
}
array_multisort($sort['Level'], SORT_DESC, $sort['OrgID'], SORT_ASC, $WlevelArray);

しかし、これは 2 つの配列を生成します。

4

1 に答える 1

3

私はあなたが何を目指していると思うかに基づいて、あなたが具体的に探しているものについて少し混乱しています。以下のコードを含めました。

アレイをセットアップします。

$example1 = array(
  "OrgID" => "XXXX",
  "DOB" => "1970-01-01",
  "PersonID" => "111111",
  "ExpDate" => "2013-07-31",
  "Level" => "WELITE",
  "Status" => "Active",
  "FName" => "Jane",
  "LName" => "Doe",
  "Checked" => null
);

$example2 = array(
  "OrgID" => "XXXX",
  "DOB" => "1970-01-01",
  "PersonID" => "222222",
  "ExpDate" => "2013-07-31",
  "Level" => "WELITE",
  "Status" => "Active",
  "FName" => "Jane",
  "LName" => "Doe",
  "Checked" => null
);

$example3 = array(
  "OrgID" => "XXXX",
  "DOB" => "1970-01-01",
  "PersonID" => "22222",
  "ExpDate" => "2013-07-31",
  "Level" => "WELITE",
  "Status" => "Active",
  "FName" => "Jane",
  "LName" => "Doe",
  "Checked" => null
);

// Initial array
$initArray = array($example1, $example2, $example3);

それでは、「並べ替え」ましょう。

$finalArray = array();
$i = 0;
foreach ($initArray as $key => $value) {
    (isset($value['Level'])) ? $finalArray[$value['Level']][$i] = $value : NULL;
    unset($finalArray[$value['Level']][$i]['Level']);
    $i++;
}

echo '<pre>', print_r($finalArray, true), '</pre>';

出力:

Array
(
    [WELITE] => Array
        (
            [0] => Array
                (
                    [OrgID] => XXXX
                    [DOB] => 1970-01-01
                    [PersonID] => 111111
                    [ExpDate] => 2013-07-31
                    [Status] => Active
                    [FName] => Jane
                    [LName] => Doe
                    [Checked] => 
                )

            [1] => Array
                (
                    [OrgID] => XXXX
                    [DOB] => 1970-01-01
                    [PersonID] => 222222
                    [ExpDate] => 2013-07-31
                    [Status] => Active
                    [FName] => Jane
                    [LName] => Doe
                    [Checked] => 
                )

            [2] => Array
                (
                    [OrgID] => XXXX
                    [DOB] => 1970-01-01
                    [PersonID] => 22222
                    [ExpDate] => 2013-07-31
                    [Status] => Active
                    [FName] => Jane
                    [LName] => Doe
                    [Checked] => 
                )

        )

)

編集

「レベル」値を変更すると、自動インクリメント変数を使用していたため、インデックスがインクリメントし続けるという問題が見つかりました$i。だから私は追加sort($finalArray[$value['Level']]);し、インデックス値をリセットしました。

foreach ($initArray as $key => $value) {
    (isset($value['Level'])) ? $finalArray[$value['Level']][$i] = $value : NULL;
    unset($finalArray[$value['Level']][$i]['Level']);
    // Add this line to fix indexes
    sort($finalArray[$value['Level']]);
    $i++;
}
于 2013-03-27T17:34:36.677 に答える