0

My array looks like this.

Array ( 
     [0] => Array ( 
                 [id] => 1 
                 [language] => English 
                 ) 
     [1] => Array ( 
                 [id] => 3 
                 [language] => Japanese 
                 ) 
     [2] => Array ( 
                 [id] => 4 
                 [language] => Italian 
                 ) 
      [3] => Array ( 
                 [id] => 5 
                 [language] => Spanish 
                 ) 
       [4] => Array (
                 [id] => 6 
                 [language] => French 
                 ) 
   )

Now what I want to do is sort this array in terms of language. So it should become like this.

Array ( 
     [0] => Array ( 
                 [id] => 1 
                 [language] => English 
                 ) 
     [1] => Array ( 
                 [id] => 6 
                 [language] => French 
                 ) 
      [2] => Array ( 
                 [id] => 4 
                 [language] => Italian 
                 ) 
       [3] => Array ( 
                 [id] => 3 
                 [language] => Japanese 
                 )  
        [4] => Array ( 
                 [id] => 5 
                 [language] => Spanish 
                 ) 
      )

Actually I did found the PHP manual helpful. It's solved. I don't know if I'm allowed to add answer to my question. But this is how I did it. I found PHP's usort very helpful for conditions like this. http://hk2.php.net/manual/en/function.usort.php

function build_sorter($key){
    return function ($a, $b) use ($key) {
        return strnatcmp($a[$key], $b[$key]);
    };
}

usort($langs, build_sorter('language'));

foreach ($langs as $value) {
    echo '<option value="'.$value['id'].'">'.$value['language'].'</option>';
}
Anonymous
  • 125
  • 3
  • 14
  • 3
    Can you share what, if anything, you've tried? SO is not a 'give me the code" kind of girl. – Jay Blanchard Jan 21 '16 at 16:18
  • 2
    I am going to make a rash and foolish suggestion - Have you looked at the manual [PHP Sort Manual](http://php.net/manual/en/function.sort.php) I know is a radical idea, but give it a try, make it your friend – RiggsFolly Jan 21 '16 at 16:25
  • Check your syntax, it doesn't look like php. – Yurich Jan 21 '16 at 16:34
  • Actually I did found the PHP manual helpful. It's solved. This is how I did it. I've edited the question with the solution. – Anonymous Jan 21 '16 at 16:40
  • @RiggsFolly the manual did help. :) – Anonymous Jan 21 '16 at 16:46
  • You can't add an answer in this case because the question was closed (because it's a duplicate of an existing question), but if it were open you'd certainly be welcome to add an answer if you've found one. StackOverflow is meant as a lasting resource, so the answer you've discovered could help someone else. – Caleb Jan 21 '16 at 17:18

1 Answers1

0

Extract the language column and sort the original array by that array:

array_multisort(array_column($array, 'language'), $array);
AbraCadaver
  • 73,820
  • 7
  • 55
  • 81