multi dimensional array sorting by string

跟風遠走 提交于 2019-11-29 09:27:41

问题


I'm breaking my head trying to figure out how to do this right, I have this multi dimensional array:

Array
(
    [0] => Array
        (
            [time] => November 1st 10:10
            [query] => movies
            [set] => 1
            [matches] => No matching results
            [results] => 5
        )

    [1] => Array
        (
            [time] => November 1st 10:10
            [query] => cinemas
            [set] => 1
            [matches] => No matching results
            [results] => 2
        )

)

In real life, there could be alot more sub-arrays, but et's say I want to sort it by "query" alphabetically, how can I achieve this?

I saw only solutions for integer type or key index, the end result, in this case, would be:

Array
    (
        [0] => Array
            (
                [time] => November 1st 10:10
                [query] => cinemas
                [set] => 1
                [matches] => No matching results
                [results] => 2
            )
        [1] => Array
            (
                [time] => November 1st 10:10
                [query] => movies
                [set] => 1
                [matches] => No matching results
                [results] => 5
            )

    )

Much appreciated, thanks.


回答1:


function querySort ($x, $y) {
    return strcasecmp($x['query'], $y['query']);
}

usort($myArray, 'querySort');



回答2:


I often use this function to sort multi dimensional arrays:

function sortmulti ($array, $index, $order, $natsort=FALSE, $case_sensitive=FALSE) {
         if(is_array($array) && count($array)>0) {
             foreach(array_keys($array) as $key) { 
                $temp[$key]=$array[$key][$index];
             }
             if(!$natsort) {
                 if ($order=='asc') {
                     asort($temp);
                 } else {    
                     arsort($temp);
                 }
             }
             else 
             {
                 if ($case_sensitive===true) {
                     natsort($temp);
                 } else {
                     natcasesort($temp);
                 }
                if($order!='asc') { 
                 $temp=array_reverse($temp,TRUE);
                }
             }
             foreach(array_keys($temp) as $key) { 
                 if (is_numeric($key)) {
                     $sorted[]=$array[$key];
                 } else {    
                     $sorted[$key]=$array[$key];
                 }
             }
             return $sorted;
         }
     return $sorted;
 }

Works a charm :)




回答3:


agreed with @Hammerite answer, But here is a shortest way of doing this kind of sorting. You can achieve the same result starting from PHP 5.3, by using an anonymous function:

 usort($myArray, function($x, $y) {
       return strcasecmp($x['query'] , $y['query']);
 });

17.1. - only syntax fix



来源:https://stackoverflow.com/questions/7963999/multi-dimensional-array-sorting-by-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!