user1676019
user1676019

Reputation: 108

Sort multidimensional array data into numerical order

I have a snippet of code that looks like this:

foreach ($final_array as $index => $data) {
    echo $data[1];
}

What I want to do is sort $data[1] into numerical order. I've tried things like asort() and natsort(), but nothing worked. Any help would be HIGHLY appreciated.

This is how my array looks:

Array
(
    [1] => Array
    (
        [0] => Awesomedude123
        [1] => 399,408
        [2] => September 16, 2012
    )

    [2] => Array
    (
        [0] => Username11
        [1] => 1,914,144
        [2] => September 16, 2012
    )

    [3] => Array
    (
        [0] => EpicSurfer
        [1] => 1,031,427
        [2] => September 16, 2012
    )
)

Upvotes: 0

Views: 74

Answers (1)

Fluffeh
Fluffeh

Reputation: 33522

You can always use usort for tricky array sorting:

function number_compare($a, $b)
{
    $t1 = str_replace( ',', '', $a[1] );
    $t2 = str_replace( ',', '', $b[1] );
    return $t1 - $t2;
}    
usort($array, 'number_compare');

Upvotes: 2

Related Questions