duanqiechui2378 2015-09-01 14:27
浏览 22
已采纳

如何按照我定义的特定顺序订购数组?

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:

enter image description here

And this is the expected output:

enter image description here

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, e.g. 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 ? 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. E.g.

Order: C,D,A,B
Array: A,B,C
  • 写回答

1条回答 默认 最新

  • doumixiang2227 2015-09-01 15:06
    关注

    Just pop off the last element of your array with array_pop() and put it again at the start of the array, e.g.

    $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().

    Means you will end up with this array, where the order are the keys and all have empty arrays as value, e.g.

    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. And at the end you can filter the empty arrays out with 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
    )
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?