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

我正在尝试将数组转换为以下内容,并根据名为 level 的值创建一个键:

 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);

但这会产生两个数组。

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 回答