SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2003
    Location
    na
    Posts
    36
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Help w/ sorting multi-dimensional array please

    Hi all,

    I'm making two queries and combining the results into an array and then trying to sort the array based on the timestamp as such:

    $r = mysql_query("SELECT * FROM table1");

    while ($a = mysql_fetch_assoc($r)) {
    $fc[$a['timestamp']]['title'] = ...
    }

    $r = mysql_query("SELECT * FROM table2");

    while ($a = mysql_fetch_assoc($r)) {
    $fc[$a['timestamp']]['title'] = ...
    }

    So if the final array is something like this:

    $fc[1111111111]['title'] = 'Dogs';
    $fc[3333333333]['title'] = 'Rabbits';
    $fc[2222222222]['title'] = 'Cats';

    I want the sorted array to look like this (SORT_DESC by timestamp):

    $fc[3333333333]['title'] = 'Rabbits';
    $fc[2222222222]['title'] = 'Cats';
    $fc[1111111111]['title'] = 'Dogs';

    The problem is that array_multisort drops the key if it's not string.

    Any suggestions please?

    Thanks

  2. #2
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    The easiest and most general way would be to just not use keys. Create a custom sort function then call it with usort();
    http://ca3.php.net/manual/en/function.usort.php


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •