PHP按值重新排序数组

时间:2013-03-27 16:40:08

标签: php arrays associative-array

我有一个数组,我想通过值来求助于数组。 这是现有的数组:

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

但这会产生两个数组。

1 个答案:

答案 0 :(得分: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++;
}