简体   繁体   English

按特定字符串按字母顺序对数组进行排序

[英]Sort array alphabetically by specific string

I have this array of plans: 我有以下计划:

Array
(
[0] => Array
    (
        [plan_id] => corporate-base
        [plan_name] => Tier 1 - NYC
    )

[1] => Array
    (
        [plan_id] => corporate-la-base
        [plan_name] => Tier 1 - LA
    )

[2] => Array
    (
        [plan_id] => corporate-sf-base
        [plan_name] => Tier 1 - SF
    )

[3] => Array
    (
        [plan_id] => corporate-core
        [plan_name] => Tier 2 - NYC
    )

[4] => Array
    (
        [plan_id] => corporate-la-core
        [plan_name] => Tier 2 - LA
    )

[5] => Array
    (
        [plan_id] => corporate-sf-core
        [plan_name] => Tier 2 - SF
    )

[6] => Array
    (
        [plan_id] => corporate-la-unlimited
        [plan_name] => Tier 3 - LA
    )

[7] => Array
    (
        [plan_id] => corporate-sf-unlimited
        [plan_name] => Tier 3 - SF
    )

[8] => Array
    (
        [plan_id] => corporate-unlimited
        [plan_name] => Tier 3 - NYC
    )

)

I want them to be grouped by city like: 我希望将它们按城市分组:

Tier 1 - LA 第1层-洛杉矶

Tier 2 - LA 第2层-洛杉矶

Tier 3 - LA 第3层-洛杉矶

Tier 1 - NYC 1级-纽约

Tier 2 - NYC 第2层-纽约

Tier 3 - NYC 第3层-纽约

Tier 1 - SF 1级-SF

Tier 2 - SF 2级-SF

Tier 3 - SF 3级-SF

I tried to use array_multisort() but it displays all Tier 1 first then Tier 2 and then Tier 3. 我尝试使用array_multisort()但它会先显示所有第1层,然后显示第2层,然后显示第3层。

Currently, I'm doing 3 foreach loops to group them into 3 cities but it's not flexible and I think it's too long. 目前,我正在执行3个foreach循环将它们分为3个城市,但它并不灵活,我认为时间太长。 What could be the quickest/easiest way to achieve this? 最快/最简单的方法是什么? Thanks! 谢谢!

You can use usort , using a sort function which extracts the city name out of the plan_name value using explode . 您可以使用usort ,使用排序功能,该功能使用explodeplan_name值中提取城市名称。 If the city names are the same, we extract the tier number so we can sort by that instead: 如果城市名称相同,我们将提取等级号,以便我们可以按其排序:

usort($array, function ($a, $b) {
    $city_a = explode('-', $a['plan_name'])[1];
    $city_b = explode('-', $b['plan_name'])[1];
    if ($city_a == $city_b) {
        // sort by tier
        $tier_a = (int)explode(' ', $a['plan_name'])[1];
        $tier_b = (int)explode(' ', $b['plan_name'])[1];
        return $tier_a - $tier_b;
    }
    else {
        return strcmp($city_a, $city_b);
    }
});

I haven't included the output as it is quite long but you can see it in this demo on 3v4l.org 我没有包含输出,因为它很长,但是您可以在3v4l.org的演示中看到它

This is an alternate version which use preg_match to extract the city and tier from each plan_name : 这是备用版本,使用preg_match从每个plan_name提取城市和等级:

usort($array, function ($a, $b) {
    preg_match('/^Tier\s*(\d+)\s*-\s*(\w+)$/', $a['plan_name'], $matches_a);
    preg_match('/^Tier\s*(\d+)\s*-\s*(\w+)$/', $b['plan_name'], $matches_b);
    // are the cities the same?
    if ($matches_a[2] == $matches_b[2]) {
        // yes, sort by tier
        return $matches_a[1] - $matches_b[1];
    }
    else {
        // no, sort by city
        return strcmp($matches_a[2], $matches_b[2]);
    }
});

Demo on 3v4l.org 3v4l.org上的演示

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

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