如何按我定义的特定顺序对数组进行排序?
How can I order an array in a specific order, which I define?
我想按我定义的特定顺序对数组进行排序。
我已经开始构建一个函数来对这些数组进行排序,但我卡住了,不知道如何解决它。
我当前的代码:
public function order_spells($champions) {
foreach(array_keys($champions) as $champion){
if(isset($champions[$champion]['Passive']) || isset($champions[$champion]['Q']) || isset($champions[$champion]['W']) || isset($champions[$champion]['E']) || isset($champions[$champion]['R'])) {
foreach(array_keys($champions[$champion]) as $Spell_Icon) {
if($Spell_Icon!='General'){
//echo $Spell_Icon;
}
}
}
}
}
这是当前数组:
这是预期的输出:
由于我坚持使用上面的当前代码,我将尝试隔离并简化我的问题并将其展示给您。
作为一个简单的例子,我有一个这样的数组:
$champions = [
"A" => 1,
"C" => 2,
"F" => 3,
"B" => 4,
"G" => 5,
"D" => 6,
"E" => 7,
];
现在我想定义数组的顺序,例如第一个键 F
,然后 D
等等......我怎样才能改变我上面的代码让它按照我想要的方式工作?或者我如何在此处订购此示例数组?
这里要提到的另一个转折是,顺序可能比数组本身有更多的元素。例如
Order: C,D,A,B
Array: A,B,C
只需使用 array_pop()
弹出数组的最后一个元素,然后将其再次放在数组的开头,例如
$arr = array_pop($champion["Ashe"]);
$champion["Ashe"] = ["E" => $arr] + $champion["Ashe"];
编辑:
根据评论,您希望按特定顺序对数组进行排序,但您不知道哪些元素确实在数组中,哪些不在数组中。所以这应该适合你:
所以基本上首先你用数组定义你想要的顺序。那你array_combine()
the array with another array, which you fill up with empty values with array_fill()
.
意味着你最终会得到这个数组,其中顺序是键,所有的值都是空数组,例如
Array (
[F] => Array ( )
[A] => Array ( )
[B] => Array ( )
[G] => Array ( )
[C] => Array ( )
[E] => Array ( )
[D] => Array ( )
)
然后你可以将此数组与 array_replace()
to order the array as you want it. And at the end you can filter the empty arrays out with array_filter()
一起使用。
代码:
<?php
$arr = [
"A" => 1,
"C" => 2,
"F" => 3,
"B" => 4,
"G" => 5,
"D" => 6,
"E" => 7,
];
$order = ["F", "A", "B", "G", "C", "E", "D"];
$order = array_combine($order, array_fill(0, count($order), []));
$arr = array_filter(array_replace($order, $arr));
print_r($arr);
?>
输出:
Array
(
[F] => 3
[A] => 1
[B] => 4
[G] => 5
[C] => 2
[E] => 7
[D] => 6
)
我想按我定义的特定顺序对数组进行排序。
我已经开始构建一个函数来对这些数组进行排序,但我卡住了,不知道如何解决它。
我当前的代码:
public function order_spells($champions) {
foreach(array_keys($champions) as $champion){
if(isset($champions[$champion]['Passive']) || isset($champions[$champion]['Q']) || isset($champions[$champion]['W']) || isset($champions[$champion]['E']) || isset($champions[$champion]['R'])) {
foreach(array_keys($champions[$champion]) as $Spell_Icon) {
if($Spell_Icon!='General'){
//echo $Spell_Icon;
}
}
}
}
}
这是当前数组:
这是预期的输出:
由于我坚持使用上面的当前代码,我将尝试隔离并简化我的问题并将其展示给您。
作为一个简单的例子,我有一个这样的数组:
$champions = [
"A" => 1,
"C" => 2,
"F" => 3,
"B" => 4,
"G" => 5,
"D" => 6,
"E" => 7,
];
现在我想定义数组的顺序,例如第一个键 F
,然后 D
等等......我怎样才能改变我上面的代码让它按照我想要的方式工作?或者我如何在此处订购此示例数组?
这里要提到的另一个转折是,顺序可能比数组本身有更多的元素。例如
Order: C,D,A,B
Array: A,B,C
只需使用 array_pop()
弹出数组的最后一个元素,然后将其再次放在数组的开头,例如
$arr = array_pop($champion["Ashe"]);
$champion["Ashe"] = ["E" => $arr] + $champion["Ashe"];
编辑:
根据评论,您希望按特定顺序对数组进行排序,但您不知道哪些元素确实在数组中,哪些不在数组中。所以这应该适合你:
所以基本上首先你用数组定义你想要的顺序。那你array_combine()
the array with another array, which you fill up with empty values with array_fill()
.
意味着你最终会得到这个数组,其中顺序是键,所有的值都是空数组,例如
Array (
[F] => Array ( )
[A] => Array ( )
[B] => Array ( )
[G] => Array ( )
[C] => Array ( )
[E] => Array ( )
[D] => Array ( )
)
然后你可以将此数组与 array_replace()
to order the array as you want it. And at the end you can filter the empty arrays out with array_filter()
一起使用。
代码:
<?php
$arr = [
"A" => 1,
"C" => 2,
"F" => 3,
"B" => 4,
"G" => 5,
"D" => 6,
"E" => 7,
];
$order = ["F", "A", "B", "G", "C", "E", "D"];
$order = array_combine($order, array_fill(0, count($order), []));
$arr = array_filter(array_replace($order, $arr));
print_r($arr);
?>
输出:
Array
(
[F] => 3
[A] => 1
[B] => 4
[G] => 5
[C] => 2
[E] => 7
[D] => 6
)