简体   繁体   English

如何按定义的特定顺序对数组进行排序?

[英]How can I order an array in a specific order, which I define?

I want to order an array in a specific order, which I define. 我想按定义的特定顺序对数组进行排序。

I've started building a function to order those arrays, but I'm stuck and I have no clue how to solve it. 我已经开始构建一个函数来对这些数组进行排序,但是我被困住了,不知道如何解决它。

My current code: 我当前的代码:

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

            }

        }

    }

}

This is the current array: 这是当前数组:

在此处输入图片说明

And this is the expected output: 这是预期的输出:

在此处输入图片说明

Since I'm stuck with my current code above, I will try to isolate and simplify my problem and show it to you. 由于我坚持上面的当前代码,因此我将尝试隔离并简化我的问题并向您展示。

So as a simple example I have an array like this: 因此,作为一个简单的例子,我有一个像这样的数组:

$champions = [
    "A" => 1,
    "C" => 2,
    "F" => 3,
    "B" => 4,
    "G" => 5,
    "D" => 6,
    "E" => 7,
];

Now I want to define the order of that array, eg First key F , then D and so on... How can I change my above code to get it to work as I want it to ? 现在,我想定义该数组的顺序,例如,第一个键F ,然后是D ,依此类推...如何更改上面的代码以使其按预期工作? Or how can I order this example array here? 或者如何在这里订购该示例阵列?

Another twist to mention here is, that the order maybe has more elements than the array itself. 这里要提到的另一种说法是,该顺序可能具有比数组本身更多的元素。 Eg 例如

Order: C,D,A,B
Array: A,B,C

Just pop off the last element of your array with array_pop() and put it again at the start of the array, eg 只需使用array_pop()弹出数组的最后一个元素,然后再次将其放在数组的开头,例如

$arr = array_pop($champion["Ashe"]);
$champion["Ashe"] = ["E" => $arr] + $champion["Ashe"];

EDIT: 编辑:

As from the comments you want to sort your array by a specific order, but you don't know which elements are really in the array and which aren't. 从注释开始,您想按特定顺序对数组进行排序,但是您不知道数组中确实有哪些元素,哪些不是。 So this should work for you: 因此,这应该为您工作:

So basically first you define the order which you want with an array. 因此,基本上首先您要使用数组定义所需的顺序。 Then you array_combine() the array with another array, which you fill up with empty values with array_fill() . 然后,用另一个数组array_combine()将该数组填充为array_fill()空值。

Means you will end up with this array, where the order are the keys and all have empty arrays as value, eg 意味着您将最终获得此数组,其中的顺序是键,并且所有值都为空数组,例如

Array (
    [F] => Array ( )  
    [A] => Array ( )
    [B] => Array ( )
    [G] => Array ( )    
    [C] => Array ( )
    [E] => Array ( )
    [D] => Array ( )   
)

Then you can use this array with array_replace() to order the array as you want it. 然后,可以将此数组与array_replace() ,以根据需要对数组进行排序。 And at the end you can filter the empty arrays out with array_filter() . 最后,您可以使用array_filter()过滤出空数组。

Code: 码:

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

?>

output: 输出:

Array
(
    [F] => 3
    [A] => 1
    [B] => 4
    [G] => 5
    [C] => 2
    [E] => 7
    [D] => 6
)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM