Page 1 of 1

Sorting an array of arrays

Posted: Mon Mar 29, 2010 2:13 pm
by iankent
This is possibly a silly question, but I dont seem to be able to get it to work. I'm trying to sort an array of arrays that looks much like this:

Code: Select all

$arr = array(
    1    =>    array(
        'title'    =>    'something',
        'id'       =>    '1952378',
    ),
    2    =>    array(
        'title'    =>    'blah blah blah',
        'id'       =>    '3427682',
    ),
    10    =>  array(
        'title'    =>    'something else',
        'id'       =>    '9827324',
    ),
);
 
If I then add another element to the array, like this:

Code: Select all

$arr[5] = array(
    'title'    =>    'new entry',
    'id'       =>    '1234567',
);
 
I end up with an array whose elements are numbered 1, 2, 10, 5. I'd like to reorder that so its 1, 2, 5, 10. I've tried using sort and asort with no luck, whatever I try and do it orders the arrays by the contents of 'title' and not by the number they're assigned. It doesn't matter if the number assigned (1, 2, 5, 10) is lost during the sort, just as long as it sorts by those and not by elements inside the arrays.

Thanks :)

Re: Sorting an array of arrays

Posted: Mon Mar 29, 2010 2:20 pm
by pickle

Re: Sorting an array of arrays

Posted: Mon Mar 29, 2010 2:23 pm
by iankent
lol, seems so obvious now! thanks :)