Array sort function in PHP

前端 未结 3 976
一向
一向 2020-12-22 12:04

I defined a myArr = (\"Apr\",\"Mar\",\"May\"). Is there a sorting function to sort the array members by nature month sequence otherwise by alphabet.

Th

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-22 12:18

    No. there is no such a function. But.... you still can solve your problem gracefully. You should associate months names with numbers (1 is for Jan, 2 is for Feb and so on). This is the most common approach in programming.

    Define your array as:

    $myArr = array(1=>"Jan", 2=>"Feb" ... )
    

    And then you can sort your $myArr by keys (ksort):

    ksort($myArr);
    var_export($myArr);
    

    That would sort your array in ascendant order by keys.

提交回复
热议问题