简体   繁体   English

如何按字母数字值对多维数组进行排序?

[英]How to sort a multidimensional array by alphanumeric value?

I have an array like this below,我有一个像下面这样的数组,

Array
(
    [2] => Array
        (
            [id] => 75
            [program] => Apr 2020-Nov 2020
        )

    [1] => Array
        (
            [id] => 73
            [program] => Feb 2016-Aug 2020
        )

    [0] => Array
        (
            [id] => 72
            [program] => May 2020-Dec 2020
        )

)

The resultant array should be结果数组应该是

Array
(


    [1] => Array
        (
            [id] => 73
            [current_program] => Feb 2016-Aug 2020
        )
    [2] => Array
        (
            [id] => 75
            [current_program] => Apr 2020-Nov 2020
        )

    [0] => Array
        (
            [id] => 72
            [current_program] => May 2020-Dec 2020
        )

)

It should sort based on the year.它应该根据年份排序。 I have tried to achieve by "strnatcasecmp", but the array is sorting by alphabet not by the numeric value in it我试图通过“strnatcasecmp”来实现,但数组是按字母排序的,而不是按其中的数值排序

    usort($programp, function($a, $b) {
        return strnatcasecmp($a['program'], $b['program']);
    });

Any help would be appreciated!任何帮助,将不胜感激!

Thanks谢谢

You need to convert the first month and year into a timestamp, sort that thereby sorting the original:您需要将第一个月和第一年转换为时间戳,对其进行排序,从而对原始进行排序:

foreach($programp as $values) {
    $starts[] = strtotime(explode('-', $values['program'])[0]);
} 
array_multisort($starts, $programp);

Before sorting the $starts array would look like this, easy to sort:在排序之前$starts数组看起来像这样,很容易排序:

Array
(
    [0] => 1585692000
    [1] => 1454281200
    [2] => 1588284000
)

You probably want some error checking to make sure $values['program'] is not empty etc...您可能需要一些错误检查以确保$values['program']不为空等...

//Sort indexes so the keys are 0,1,2 instead of 1,2,0
//This is important when sorting down below with asort()
$arr = array_values($arr); 

//Go through every "program" (dateinterval in your array)
//and make a new array in the format year-monthnr
//Date parse returns the number of jan(1),feb(2),mar(3) etc..
$year_month = [];
foreach(array_column($arr,'program') as $key => $item) {       
    $year = explode(' ', explode('-',$item)[0])[1];
    $month = date_parse(explode(' ', explode('-',$item)[0])[0])['month'];
    $year_month[] = $year . '-' . $month;
}

//Sort with mainted indexes (keys)
asort($year_month); 

//Create new array "mapped" keys to the original array $arr
$new_arr = [];
foreach($year_month as $key=>$item) {
    $new_arr[] = $arr[$key];
}

Output of $new_arr would be: $new_arr将是:

Array
(
    [0] => Array
        (
            [id] => 73
            [program] => Feb 2016-Aug 2020
        )

    [1] => Array
        (
            [id] => 75
            [program] => Apr 2020-Nov 2020
        )

    [2] => Array
        (
            [id] => 72
            [program] => May 2020-Dec 2020
        )

)

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

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