【问题标题】:Is it possible to order an array alphabetically in Laravel?Is it possible to order an array alphabetically in Laravel?
【发布时间】:2022-12-02 03:05:27
【问题描述】:

I am new to Laravel and I was wondering if I can order the following multidimensional array with countries alphabetically? So I want all the countries inside the continents to be ordered alphabetically.

  "EU" => array:9 
    0 => "NL"
    1 => "BE"
    3 => "FR"
    4 => "DE"
    5 => "ES"
    6 => "IT"
    7 => "GB"
    8 => "TR"
    9 => "DK"
  ]
  "AS" => array:2
]

【问题讨论】:

    标签: php arrays laravel alphabetical-sort


    【解决方案1】:

    It seems you need to sort an inner array by values. Here is a one way to do it.

    $arr = [
       "EU" => [
            0 => "NL",
            1 => "BE",
            3 => "FR",
            4 => "DE",
            5 => "ES",
            6 => "IT",
            7 => "GB",
            8 => "TR",
            9 => "DK",
      ],
      "AS" => ["AB", "AA"]
    ];
    
    foreach($arr as $continent => $countries) {
        asort($countries);
        $arr[$continent] = $countries;
    }
    
    
    var_dump($arr);
    

    【讨论】:

    • Can I also remove the duplicate countries in that foreach?
    • @Elisaveta sure you can!. You need to change this line $arr[$continent] = $countries; to this $arr[$continent] = array_unique($countries)
    【解决方案2】:

    Laravel has nothing to do with these stuff. You should use PHP functions and here's an example:

    $arr = [
        0 => "NL",
        1 => "BE",
        3 => "FR",
        4 => "DE",
        5 => "ES",
        6 => "IT",
        7 => "GB",
        8 => "TR",
        9 => "DK",
    ];
    
    sort($arr);
    
    print_r($arr);
    

    The result:

    Array
    (
        [0] => BE
        [1] => DE
        [2] => DK
        [3] => ES
        [4] => FR
        [5] => GB
        [6] => IT
        [7] => NL
        [8] => TR
    )
    

    【讨论】:

    • Hey, thank you for your answer. Unfortunately, this does not work as it is multidimensional array.
    • What about using usort function? php.net/manual/en/function.usort.php
    • Still does not work, I already tried those options
    • Please edit your question and explain what input you have, and what output you want. This is how we can help you better.
    【解决方案3】:

    Since you are looking for a Laravel answer, you might be interested in the Arr::sortRecursive() helper for a short and fluent implementation. Let me demonstrate the code for your data while also expending your data a bit more to the my point across:

    use IlluminateSupportArr;
    
    $array = [
      "EU" => [
        "NL", "BE", "FR", "DE", "ES", "IT", "GB", "TR", "DK",
      ],
      "AS" => [
        "JA", "IN", "CH",
      ],
      "NA" => [
        "US", "ME", "CA",
      ],
    ];
     
    $sorted = Arr::sortRecursive($array);
    
    /*
    [
      "AS" => [
        "CH", "IN", "JA",
      ],
      "EU" => [
        "BE", "DE", "DK", "ES", "FR", "GB", "IT", "NL", "TR",
      ],
      "NA" => [
        "CA", "ME", "US",
      ],
    ]
    */
    

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-27
      • 2022-12-02
      • 2022-12-02
      • 2022-12-27
      • 2022-12-02
      • 2022-12-28
      • 1970-01-01
      • 2022-12-27
      相关资源
      最近更新 更多