duanhuan5409 2016-06-20 10:26
浏览 25
已采纳

Laravel / PHP语言代码排序

I have some languages stored in my database as follows:

id | code  | name 
--------------------------------------
1  | en_GB | English, United Kingdom
2  | pt_BR | Portugese, Brazil
3  | it_IT | Italian, Italy
4  | hi_IN | Hindi, India
5  | es_BR | Spanish, Brazil
6  | ur_IN | Urdu, India

I want to get these results and sort (or sort after I get all results), so I get the results first by ordered Country Code (second part of code) and then by Locale Code (first part of code) so:

$languages = [
    0 => [
       'code' => 'es_BR'
    ],
    1 => [
       'code' => 'pt_BR'
    ],
    2 => [
       'code' => 'en_GB'
    ],
    3 => [
       'code' => 'hi_IN'
    ],
    4 => [
       'code' => 'ur_IN'
    ],
    5 => [
       'code' => 'it_IT'
    ]
];

I've tried looping through each language and taking out the second part and storing an array of the Country Codes and then sorting them using asort($countryCodes);, which did sort by Country Code (GB), but I'm stuck sorting that list out by Locale Code (en).

Any help will be appreciated, thanks!

  • 写回答

4条回答 默认 最新

  • duanjianao0592 2016-06-20 11:19
    关注

    I have come up with the following and it has given me what I needed:

    $languages = $this->getOrderedLanguages();
    
    protected function getOrderedLanguages() 
    {
        $languages = Language::orderBy('code')->get();
    
        // Sort out the other languages by country code (e.g. sort by GB in code en_GB)
        // Create array for each country code and store array key from original list
        $countryCodes = [];
        foreach($languages as $key => $language) {
            $countryCodes[$language->locale_code][] = [
                'id' => $key,
                'language' => $language->language_code
            ];
        }
        ksort($countryCodes);
    
        $orderedLanguages = [];
    
        // Go through each country code and sort the languages in there by locale code (e.g. sort by en in code en_GB)
        foreach($countryCodes as &$countryCode) {
            asort($countryCode);
            foreach($countryCode as $language) {
                $orderedLanguages[] = $allLanguages[$language['id']];
            }
        }
    
        // Return ordered array
        return $orderedLanguages;
    }
    

    This has given me the following result:

    $languages = [
        0 => [
            'code' => 'es_BR'
        ],
        1 => [
            'code' => 'pt_BR'
        ],
        2 => [
            'code' => 'en_GB'
        ],
        3 => [
            'code' => 'hi_IN'
        ],
        4 => [
            'code' => 'ur_IN'
        ],
        5 => [
            'code' => 'it_IT'
        ]
    ];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据