Reputation: 2877
I found lots of info about sorting associative arrays but little about sorting non-associative ones. My array is structured/populated this way:
$my_array = array();
$my_array[0][0] = 'whatever3';
$my_array[0][1] = 3
$my_array[1][0] = 'whatever2';
$my_array[1][1] = 2
$my_array[2][0] = 'whatever1';
$my_array[2][1] = 1
I want to sort it by the second value to get:
$my_array[0][0] = 'whatever1';
$my_array[0][1] = 1;
$my_array[1][0] = 'whatever2';
$my_array[1][1] = 2;
$my_array[2][0] = 'whatever3';
$my_array[2][1] = 3;
How can this be achieved considering my array isn't associative?
Upvotes: 0
Views: 247
Reputation: 20015
What about:
usort($combined, function ($a, $b) { return $a[1] - $b[1]; });
With usort
you provide a custom comparison function that must return:
0
, if the elements must be considered equal.In this case, we choose to compare the second element of each item of $combined
array.
Upvotes: 2