Jump to content

[SOLVED] Sorting 2D array by inner array's key


thiggins09

Recommended Posts

What is the most efficient way to do this for a fairly big array?

 

For example:

 

$arr[0][0]='apple';
$arr[0][1]='24';
$arr[1][0]='orange';
$arr[1][1]='2';
$arr[2][0]='banana';
$arr[2][1]='3451';

 

What would be the best way to sort that array by the inner array's 1 key?

 $arr[0][0]='apple';
$arr[0][1]='24';
$arr[1][0]='orange';
$arr[1][1]='2';
$arr[2][0]='banana';
$arr[2][1]='3451';

function compare($ar1, $ar2){
  return ($ar1[1] > $ar2[1]);
}

uksort($arr, "compare");

foreach($arr as $sub){
  echo "$sub[0], $sub[1]<br>";
}

 

What is that, your homework problem?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.