简体   繁体   中英

Multidimensional array sort

How can i sort "award_year" whereby all the other key will follow the sort order. All key names except "award_year" will vary in name as well as total keys as well?

    array(7) {
      ["award_year"]=>
      array(2) {
        [0]=>
        string(7) "1999-01"
        [1]=>
        string(7) "2010-02"
      }
      ["award_title_user"]=>
      array(2) {
        [0]=>
        string(1) "2"
        [1]=>
        string(2) "tt"
      }
      ["award_description_user"]=>
      array(2) {
        [0]=>
        string(1) "2"
        [1]=>
        string(3) "ddd"
      }
      ["award_title_lang1"]=>
      array(2) {
        [0]=>
        string(2) "ms"
        [1]=>
        string(3) "ms2"
      }
      ["award_description_lang1"]=>
      array(2) {
        [0]=>
        string(2) "ms"
        [1]=>
        string(3) "ms2"
      }
    }

Please advice.

Probably not the fastest way but:

sort($arrayin['award_year']);
$arrayout = array();
foreach( $arrayin["award_year"] as $key => $value ){
  array_push($arrayout['award_year'], $array_in['award_year'][$key]);
  array_push($arrayout['award_title_user'], $array_in['award_title_user'][$key]);
  //etc
}

for PHP 5.3:

//Sample Multi-dimensional Array ($myArray)
Array
(
    [0] => Array
        (
            [award_year]       => 2012
            [award_title_user] => test2
        )

    [1] => Array
        (
            [award_year]       => 2011
            [award_title_user] => test1
        )

)

usort($myArray, function($a, $b) {
    return $a['award_year'] - $b['award_year'];
});

print_r($myArray);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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