0

Good day.

We have array:

array(3) {
  [1]=>
  array(9) {
    [1]=>
    string(12) "aaandroid.ru"
    [2]=>
    string(1) "0"
    [3]=>
    string(1) "0"
    [4]=>
    string(1) "0"
    [5]=>
    string(1) "0"
    [6]=>
    string(1) "0"
    [7]=>
    string(5) "Test2"
    [8]=>
    string(10) "2012-03-27"
    [9]=>
    string(10) "2013-04-29"
  }
  [2]=>
  array(9) {
    [1]=>
    string(7) "aaga.ru"
    [2]=>
    string(1) "0"
    [3]=>
    string(1) "0"
    [4]=>
    string(1) "0"
    [5]=>
    string(1) "0"
    [6]=>
    string(1) "0"
    [7]=>
    string(8) "Test1"
    [8]=>
    string(10) "2008-02-21"
    [9]=>
    string(10) "2013-04-29"
  }
  [3]=>
  array(9) {
    [1]=>
    string(10) "aatrakc.ru"
    [2]=>
    string(1) "0"
    [3]=>
    string(1) "0"
    [4]=>
    string(1) "0"
    [5]=>
    string(1) "0"
    [6]=>
    string(1) "0"
    [7]=>
    string(8) "Test3"
    [8]=>
    string(10) "2012-03-27"
    [9]=>
    string(10) "2013-04-29"
  }

Tell me please how sort data in array with key?

For example i would like get array where data sorting on element 7, ie. in result i would like get array:

array(3) {
      [1]=>
      array(9) {
    [1]=>
    string(7) "aaga.ru"
    [2]=>
    string(1) "0"
    [3]=>
    string(1) "0"
    [4]=>
    string(1) "0"
    [5]=>
    string(1) "0"
    [6]=>
    string(1) "0"
    [7]=>
    string(8) "Test1"
    [8]=>
    string(10) "2008-02-21"
    [9]=>
    string(10) "2013-04-29"
      }
    [1]=>
    string(12) "aaandroid.ru"
    [2]=>
    string(1) "0"
    [3]=>
    string(1) "0"
    [4]=>
    string(1) "0"
    [5]=>
    string(1) "0"
    [6]=>
    string(1) "0"
    [7]=>
    string(5) "Test2"
    [8]=>
    string(10) "2012-03-27"
    [9]=>
    string(10) "2013-04-29"
      }
      [3]=>
      array(9) {
        [1]=>
        string(10) "aatrakc.ru"
        [2]=>
        string(1) "0"
        [3]=>
        string(1) "0"
        [4]=>
        string(1) "0"
        [5]=>
        string(1) "0"
        [6]=>
        string(1) "0"
        [7]=>
        string(8) "Test3"
        [8]=>
        string(10) "2012-03-27"
        [9]=>
        string(10) "2013-04-29"
      }

Tell me please it really an how make it?

2
  • usort($myArray, function($a, $b) { if ($a[7] == $b[7]) { return 0; } return ($a[7] < $b[7]) ? -1 : 1; }); Commented Apr 12, 2014 at 15:53
  • how to sort the data in reverse order? Commented Apr 12, 2014 at 16:17

2 Answers 2

2

Check the PHP usort function: http://www.php.net/manual/en/function.usort.php. It provides (in place) sorting based on a callback, which you can create.

Example:

usort($myArray, function ($a, $b) {
    return strcmp($a[7], $b[7]);
});
Sign up to request clarification or add additional context in comments.

3 Comments

nice... tell me please how to sort the data in reverse order?
All you have to do is to switch the order of parameters in strcmp call. strcmp($b[7], $a[7]);
@learner Please look at the documentation in the link I pointed to. Learn the function, and you can answer your own question.
0

Continuing Hidde's answer, if you want to reverse the order of the sort you would still use usort(), but you reverse the order of the parameters in the call to strcmp(). This will reverse which of the two arrays usort() sees as the larger value.

usort($myArray, function ($a, $b) {
   return strcmp($b[7], $a[7]);
});

This uses an anonymous function, so it will only work on Php 5.3 or later. If you have to work on 5.2 define a function to use as the callback.

function mySortFunction($a, $b) {
    return strcmp($b[7], $a[7]);
}

usort($myArray, 'mySortFunction');

See:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.