SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Sorting multidimensional Arrays maintaining index associations

    I need to order an array like it was a SQL result set, so let's say I have:

    $result['domains'][0]='e.com';
    $result['domains'][1]='a.com';
    $result['domains'][2]='d.com';

    $result['active'][0]=true;
    $result['active'][1]=false;
    $result['active'][2]=true;

    How do I order the array alphabetically by the 'domains' index, while keeping associations with the 'active' index? something like 'ORDER BY 'domains' ASC' in SQL? I've tried array_multisort but it doesn't seem to keep associations and frankly I never understood how it works, it just confuses me :/
    Thanks!

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I dont know how exactly it should be but here when i test like this:
    PHP Code:
    $result['domains'][0]='e.com';
    $result['domains'][1]='a.com';
    $result['domains'][2]='d.com';

    $result['active'][0]=1;
    $result['active'][1]=2;
    $result['active'][2]=3;

    array_multisort($result['domains'],$result['active']);
    print 
    "<pre>";
    print_r($result['domains']);
    print_r($result['active']);
    print 
    "</pre>"
    it gave me this result which seems to be fine and accurate:
    Code:
    Array
    (
        [0] => a.com
        [1] => d.com
        [2] => e.com
    )
    Array
    (
        [0] => 2
        [1] => 3
        [2] => 1
    )
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    SitePoint Addict SwordsmanX's Avatar
    Join Date
    Sep 2005
    Posts
    211
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    turned out simpler than I thought!
    thanks for helping


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
  •