"1970-01-01" PersonID => -6ren">
gpt4 book ai didi

PHP 按值重新排序数组

转载 作者:搜寻专家 更新时间:2023-10-31 20:43:58 24 4
gpt4 key购买 nike

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

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

但这会产生两个数组。

最佳答案

根据我认为您的目标,我对您的具体目标感到有些困惑,我在下面包含了代码。

设置数组:

$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++;
}

关于PHP 按值重新排序数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15664367/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com