Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have an array that needs to be sorted by the third level.

I need to order the array by the fullname value. Is this possible?

$array = array(
    'family1' => array(
        'family_data' => array(
            'fullname' => 'Samuel Smith',
            '...' => '...'
        )
    ),
    'family2' => array(
        'family_data' => array(
            'fullname' => 'John Doe',
            '...' => '...'
        )
    ),
    'family3' => array(
        'family_data' => array(
            'fullname' => 'Adrian Chan',
            '...' => '...'
        )
    )
);

Easier reading:

Array
(
    [family1] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Samuel Smith
                    [...] => ...
                )

        )

    [family2] => Array
        (
            [family_data] => Array
                (
                    [fullname] => John Doe
                    [...] => ...
                )

        )

    [family3] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Adrian Chan
                    [...] => ...
                )

        )

)

The desired result would be:

Array
(
    [family3] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Adrian Chan
                    [...] => ...
                )

        )

    [family2] => Array
        (
            [family_data] => Array
                (
                    [fullname] => John Doe
                    [...] => ...
                )

        )

    [family1] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Samuel Smith
                    [...] => ...
                )

        )

)
share|improve this question
    
(The code before your recent edit would have been better, as we can copy-paste it into our editor to test. (Which I did.)) –  Dogbert May 16 '13 at 8:17
    
stackoverflow.com/questions/15062210/how-do-i-sort-the-follwing-array –  Prasanth Bendra May 16 '13 at 8:18
    
@Dogbert Thanks, I'll remember that for next time –  Ben Sinclair May 16 '13 at 8:19

4 Answers 4

up vote 3 down vote accepted

Use uasort

uasort — Sort an array with a user-defined comparison function and maintain index association

uasort($array, function($a, $b) {
    return strcmp($a['family_data']['fullname'], $b['family_data']['fullname']);
});

Test:

$array = array(
    'family1' => array(
        'family_data' => array(
            'fullname' => 'Ben Smith',
            '...' => '...',
            '...' => '...'
        ),
        '...' => '...',
        '...' => '...'
    ),
    'family2' => array(
        'family_data' => array(
            'fullname' => 'John Doe',
            '...' => '...',
            '...' => '...'
        ),
        '...' => '...',
        '...' => '...'
    ),
    'family3' => array(
        'family_data' => array(
            'fullname' => 'Aen Smith',
            '...' => '...',
            '...' => '...'
        ),
        '...' => '...',
        '...' => '...'
    ),
);

uasort($array, function($a, $b) {
    return strcmp($a['family_data']['fullname'], $b['family_data']['fullname']);
});

print_r($array);

Output:

Array
(
    [family3] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Aen Smith
                    [...] => ...
                )

            [...] => ...
        )

    [family1] => Array
        (
            [family_data] => Array
                (
                    [fullname] => Ben Smith
                    [...] => ...
                )

            [...] => ...
        )

    [family2] => Array
        (
            [family_data] => Array
                (
                    [fullname] => John Doe
                    [...] => ...
                )

            [...] => ...
        )

)
share|improve this answer

Try this :

$sort = array();
foreach($your_array as $k=>$v) {
    $sort['fullname'][$k] = $v['family_data']['fullname'];
}

array_multisort($sort['fullname'], SORT_ASC, $your_array);


echo "<pre>";
print_r($your_array);
share|improve this answer

http://www.php.net/manual/en/function.array-multisort.php Take a look at example 3, I think that would help you out.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.