SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Does array_multisort mantain key association for you?

    Does array_multisort mantain key association for you?

    The manual says:
    array_multisort() can be used to sort several arrays at once or a multi-dimensional array according by one of more dimensions. It maintains key association when sorting.
    It doesn't for me. They keys are lost and it renumbers them from 0 to n. I have PHP 4.3.4

  2. #2
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    If you want, you can post a simple example of your code, and I'll try it out and see what it does for me.

    --ed

  3. #3
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?
    //names array. keys are user id's
    $names[5]  = 'Peter';
    $names[7]  = 'Jim';
    $names[11] = 'Martin';
    $names[33] = 'John';
    $names[34] = 'Robert';

    //age array. keys are user id's
    $age[5]    = 15;
    $age[7]    = 19;
    $age[11]   = 54;
    $age[33]   = 29;
    $age[34]   = 17;

    //sort names array by ages
    array_multisort($age$names);

    var_dump($names);

    // returns the following for me
    //
    // array
    //   0 => 'Peter'     (age 15) <-- i just put the ages here                   
    //   1 => 'Robert'    (age 17)     to demonstrate that array_multisort        
    //   2 => 'Jim'       (age 19)     sorted everything correctly (expect that it
    //   3 => 'John'      (age 29)     didn't mantain key association)
    //   4 => 'Martin'    (age 54) 
    //
    //
    // _should_ return
    //
    // array
    //   5  => 'Peter'    (age 15)
    //   34 => 'Robert'   (age 17)
    //   7  => 'Jim'      (age 19)
    //   33 => 'John'     (age 29)
    //   11 => 'Martin'   (age 54)


    ?>
    thanks! I'm curious on what it outputs for you

  4. #4
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok just read this at the php.net array_multisort comments section

    It should be noted that whilst the manuel says:
    "It[array_multisort] maintains key association when sorting".
    This is only Partially true.

    It will maintain string keys and associates them properly.
    However if you are using numeric keys, it will reset them to 0.

  5. #5
    Non-Member coo_t2's Avatar
    Join Date
    Feb 2003
    Location
    Dog Street
    Posts
    1,819
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by spybreak
    I don't guess it matters now, but I got the same output as you.

    --ed

  6. #6
    SitePoint Zealot spybreak's Avatar
    Join Date
    Apr 2003
    Location
    Germany
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks though !

    I reported this at bugs.php.net


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
  •