How to sort arrays inside an array?

I need to sort arrays inside an array array based on one of the values โ€‹โ€‹of the array array.

For instance:

$data = array( array( 1, "Article One", 132, 12402773, 3 ), array( 2, "Article Two", 251, 12519283, 5 ), array( 3, "Article Three", 107, 12411321, 3 ), array( 4, "Article Four", 501, 12228135, 4 ) ); 

By default, if I print the second element of each array:

  • Article One
  • Article Two
  • Article Three
  • Article Four

I need to sort it in descending order by the third element of the child array.

So it will be like this:

  • Article Four
  • Article Two
  • Article One
  • Article Three

Since 501> 251> 132> 107.

Any suggestion?

+4
source share
2 answers

I usually use usort() for this:

 function compare($a, $b) { return ($a[2] > $b[2]); } usort($data, 'compare'); 
+7
source
+2
source

All Articles