-1

So I have an array like this and want to sort it so that the best average goes first

$first_array = array(
     0 => array(
          'name' => 'John Doe'
          'average' => 7.2
     ),
     1 => array(
          'name' => 'Peter Parker'
          'average' => 8.1
     ), 
     2 => array(
           'name' => 'Albert Einstein'
           'average' => 5.6
     ));

I want it to be like this

$final_array = array(
     0 => array(
          'name' => 'Peter Parker'
          'average' => 8.1
     ),
     1 => array(
          'name' => 'John Doe'
          'average' => 7.2
     ), 
     2 => array(
           'name' => 'Albert Einstein'
           'average' => 5.6
     ));

I want to order them depending on the average, but I dont know how to do it since it's a MultiDimensional array, I couldn't find anything to help myself since I'm a little bit newbie at this..

Thanks alot

MiGu3X
  • 109
  • 15

1 Answers1

1

Use array_multisort() function, like this:

array_multisort($first_array, SORT_DESC);

Or, use usort() function, like this:

function sort_by_average($a, $b) {
    if ($a['average'] == $b['average']) {
        return 0;
    }
    return ($a['average'] < $b['average']) ? 1 : -1;
}

usort($first_array, "sort_by_average");
Rajdeep Paul
  • 16,801
  • 3
  • 16
  • 34
  • It didn't worked, I have another values on the array also, it sorted the arrays by the Names and not the float/double numbers. – MiGu3X Feb 15 '16 at 18:07
  • @MiGu3X It works as per your question. What other values are there in your array? – Rajdeep Paul Feb 15 '16 at 18:10
  • It didn't worked for me, it orders the values by its names, it looks like this: array(5633) { [0] => array(2) { 'name' => string(21) "Zyskindowicz Agustin " 'average' => double(6.67) } [1] => array(2) { 'name' => string(16) "Zyserman Sheila " 'average' => double(7.05) } [2] => array(2) { 'name' => string(20) "Zylbersztein Stefan " 'average' => double(8.84) } [3] => array(2) { 'name' => string(17) "Zylberberg Pedro " 'average' => double(6.21) } Starting from Z to A, by alphabet – MiGu3X Feb 15 '16 at 18:11
  • @MiGu3X According to the documentation, while sorting multidimensional array *associative (string) keys will be maintained, but numeric keys will be re-indexed.* How do you want to sort the arrays? By *average* or by *name*? You can see a demo here, [https://ideone.com/4cCwKR](https://ideone.com/4cCwKR) – Rajdeep Paul Feb 15 '16 at 18:17
  • By average, but it sorts it by name... I don't know why – MiGu3X Feb 15 '16 at 18:21
  • @MiGu3X No, it doesn't sort by *name*, it sorts by *average*. I've included a demo in my [comment](http://stackoverflow.com/questions/35415783/sort-array-inside-array-php#comment58533467_35416014). – Rajdeep Paul Feb 15 '16 at 18:23
  • Well, it's working incorrectly, I've done all you've told me and still sorts by name... Is there any other way? I have an array of 5600... – MiGu3X Feb 15 '16 at 18:26
  • @MiGu3X [`usort()`](http://php.net/manual/en/function.usort.php) function is another way. Here's a demo, [https://ideone.com/Y6sOfw](https://ideone.com/Y6sOfw) – Rajdeep Paul Feb 15 '16 at 18:50
  • thanks alot man! The "usort()" way worked perfectly! Love you a lot! Could you please answer the question with that way? Genious! – MiGu3X Feb 15 '16 at 18:54
  • @MiGu3X Glad to know it works. I've updated my answer. Please *accept* the answer if it resolved your issue. :-) – Rajdeep Paul Feb 15 '16 at 18:59